現在お問い合わせの履歴が見れるページを作成してまして、そこでの画面遷移がうまくいっていません。
###構想
お問い合わせページから履歴を見るのリンクを押す
→履歴ページに遷移
→戻るボタンを押す
→お問い合わせページ
→再度履歴へのリンクを押す
→お問い合わせページに遷移する
このような形をイメージしています。
###現状
お問い合わせページから履歴へのリンクを押す
(履歴へのパスはアクション名
のみです。)→対象のControllerが無いというエラーになる
なので、
履歴へのリンクのパスをController名/アクション名
に変更すると
お問い合わせページから履歴へのリンクを押す
→履歴ページに遷移する
→戻るを押す
→お問い合わせページに遷移する
→再度履歴へのリンクを押す
→エラーになる
というようになります。
###現状のコード
routes
1$routes->connect('/inquires', ['controller' => 'Inquires', 'action' => 'index']);
Controller
1<?php 2 3namespace App\Controller; 4 5use App\Controller\AppController; 6use Cake\Network\Exception\BadRequestException; 7use Cake\ORM\TableRegistry; 8use Cake\Core\Configure; 9use Cake\Event\Event; 10 11 12/** 13* Inquires Controller 14*/ 15class InquiresController extends AppController 16{ 17 public function initialize() 18 { 19 parent::initialize(); 20 $this->loadModel('Inquires'); 21 } 22 23 public function index() 24 { 25 $inquires = $this->Inquires->newEntity(); 26 $this->set(compact('inquires')); 27 28 if ($this->request->is('post')) { 29 // POSTされた場合 30 $userInquires = $this->Inquires->newEntity($this->request->data, [ 31 'validate' => 'default' 32 ]); 33 34 if ($userInquires->errors()) { 35 /* 36 * バリデーションに引っかかった場合、エラーメッセージを表示し 37 * 入力内容をセット後、入力画面に遷移する。 38 */ 39 $this->Flash->error('入力内容に不備があります'); 40 $this->set('inquires', $userInquires); 41 return $this->render($this->request->action, 'default'); 42 } 43 44 // 入力内容をセッションに格納し、確認画面へリダイレクトする 45 $this->request->session()->write('inquires', $userInquires); 46 return $this->redirect('/Inquires/confirm'); 47 } 48 49 return $this->render($this->request->action, 'default'); 50 } 51 52 public function confirm() 53 { 54 // セッションから入力内容取得 55 $userInquires = $this->request->session()->read('inquires'); 56 57 if (empty($userInquires)) { 58 // セッションの情報を取得できなかった場合は、例外を投げる 59 throw new BadRequestException('セッションの情報を取得できませんでした'); 60 return; 61 } 62 63 $this->set('inquires', $userInquires); 64 $this->render($this->request->action, 'default'); 65 } 66 67 public function complete() 68 { 69 // セッションから入力内容取得 70 $userInquires = $this->request->session()->consume('inquires'); 71 72 if (empty($userInquires)) { 73 // セッションの情報を取得できなかった場合は、例外を投げる 74 throw new BadRequestException('セッション情報を取得できませんでした'); 75 return; 76 } 77 78 $userInquires->user_id = $this->Auth->user()['id']; 79 80 if (!$this->Inquires->save($userInquires)) { 81 return $this->Flash->error('入力内容を保存できませんでした'); 82 } 83 84 // 入力内容を保存する 85 $this->Inquires->save($userInquires); 86 $this->render($this->request->action, 'default'); 87 } 88 89 public function history() 90 { 91 $userId = $this->Auth->user()['id']; 92 93 $inquires = $this->Inquires 94 ->find() 95 ->where(['user_id' => $userId]) 96 ->order(['created_at' => 'desc']) 97 ->all(); 98 99 foreach ($inquires as $inquire) { 100 if (!empty($inquire)) { 101 $inquireHistories[] = $inquire; 102 } 103 } 104 $this->set(compact('inquireHistories')); 105 106 $this->render($this->request->action, 'default'); 107 } 108} 109
お問い合わせページのView
View
1<div> 2 <?= $this->Form->create($inquires, [ 3 'type' => 'post', 4 'url' => [ 5 'controller' => 'Inquires', 6 'action' => 'index' 7 ], 8 'context' => [ 9 'validate' => 'default' 10 ] 11 ]) ?> 12 13 <h2>お問い合わせフォーム</h2> 14 15 <a href="history">履歴を見る</a> 16 17 <?= $this->Flash->render() ?> 18 19 <div> 20 <p>タイトル</p> 21 <?= $this->Form->input('title', [ 22 'type' => 'text', 23 'label' => false 24 ]) ?> 25 </div> 26 27 <div> 28 <p>お問い合わせ内容</p> 29 <?= $this->Form->input('text', [ 30 'type' => 'textarea', 31 'label' => false 32 ]) ?> 33 </div> 34 35 <div> 36 <?= $this->Form->submit() ?> 37 </div> 38 39 <?= $this->Form->end() ?> 40</div>
履歴ページのView
<div> <h2>お問い合わせ履歴</h2> <div> <?php foreach($inquireHistories as $inquireHistory): ?> <div style="border-bottom: 1px solid black"> <p>タイトル:<?= $inquireHistory->title ?></p> <p>お問い合わせ内容:<?= $inquireHistory->text ?></p> <p>お問い合わせ日時:<?= date('Y-m-d', strtotime($inquireHistory->created_at)) . " " . str_replace('-', ':', date('H-i', strtotime($inquireHistory->created_at))) ?></p> </div> <?php endforeach; ?> </div> <div> <a href="index">戻る</a> </div> </div>
###質問
- 構想から現状になってしまう原因はなんなのでしょうか?
- 対応策はどのようにすれば良いでしょうか?
よろしくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/02 15:57