###Blogチュートリアルにおけるリダイレクト処理
CakePHPブログチュートリアルを元に実装してみたのですが、追加・編集・削除の後にリダイレクト処理を入れているのですが、画面が白いままで例えば以下のURLからindexへ遷移してくれません。
http://localhost/cake/posts/add
indexの画面に戻って更新をかけるとデータが追加されているのでそこら辺の処理は大丈夫だと思うのですが、何が原因だと思われるでしょうか?
後、追加作業が成功した際のメッセージも表示されない状況です。
環境は以下の環境で行いました。
- Mac
- MAMP
- MySQL
- CakePHP(Ver.2.6.4)
以下に念の為PostsConroller.phpのソースを載せておきます。
lang
1<?php 2//File: /app/Controller/PostsController.php 3class PostsController extends AppController { 4 public $helpers = array('Html', 'Form', 'Session'); 5 public $components = array('Session'); 6 7 public function index() { 8 $this->set('posts', $this->Post->find('all')); 9 } 10 11 public function view($id = null) { 12 if(!$id) { 13 throw new NotFoundException(__('Invalid post')); 14 } 15 16 $post = $this->Post->findById($id); 17 if(!$post) { 18 throw new NotFoundException(__('Invalid post')); 19 } 20 $this->set('post', $post); 21 } 22 23 public function add() { 24 if($this->request->is('post')) { 25 $this->Post->create(); 26 if($this->Post->save($this->request->data)) { 27 $this->Session->setFlash(__('Your post has been saved.')); 28 return $this->redirect(array('action' => 'index')); 29 } 30 $this->Session->setFlash(__('Unable to add your post.')); 31 } 32 } 33 34 public function edit($id = null) { 35 if(!$id) { 36 throw new NotFoundException(__('Invalid post')); 37 } 38 39 $post = $this->Post->findById($id); 40 if (!$post) { 41 throw new NotFoundException(__('Invalid post')); 42 } 43 44 if ($this->request->is(array('post','put'))) { 45 $this->Post->id = $id; 46 if ($this->Post->save($this->request->data)) { 47 $this->Session->setFlash(__('Your post has been updated.')); 48 return $this->redirect(array('action' => 'index')); 49 } 50 $this->Session->setFlash(__('Unable to update your post.')); 51 } 52 53 if (!$this->request->data) { 54 $this->request->data = $post; 55 } 56 } 57 58 public function delete($id) { 59 if ($this->request->is('get')) { 60 throw new MethodNotAllowedException(); 61 } 62 63 if ($this->Post->delete($id)) { 64 $this->Session->setFlash( 65 __('The post with id: %s has been deleted.', h($id)) 66 ); 67 } else { 68 $this->Session->setFlash( 69 __('The post with id: %s could not be deleted.', h($id)) 70 ); 71 } 72 73 return $this->redirect(array('action' => 'index')); 74 } 75} 76?>
2015/7/2現在エラーは吐かれていないので一旦削除致します。
追記
こちらがindex.ctpのソースファイルです。
lang
1<!-- File: /app/View/Posts/index.ctp --> 2 3<h1>Blog posts</h1> 4<p><?php echo $this->Html->link('Add Post', array('action' => 'add')); ?></p> 5<table> 6 <tr> 7 <th>Id</th> 8 <th>Title</th> 9 <th>Actions</th> 10 <th>Created</th> 11 </tr> 12 13 <!-- ここから、$posts配列をループして、投稿記事の情報を表示 --> 14 15<?php foreach ($posts as $post): ?> 16 <tr> 17 <td><?php echo $post['Post']['id']; ?></td> 18 <td> 19 <?php echo $this->Html->link($post['Post']['title'], array('action' => 'view', $post['Post']['id'])); ?> 20 </td> 21 <td> 22 <?php echo $this->Form->postLink( 23 'Delete', 24 array('action' => 'delete', $post['Post']['id']), 25 array('confirm' => 'Are you sure?')); 26 ?> 27 <?php echo $this->Html->link('Edit', array('action' => 'edit', $post['Post']['id'])); ?> 28 </td> 29 <td><?php echo $post['Post']['created']; ?></td> 30 </tr> 31<?php endforeach; ?> 32 33</table> 34
そして以下が使用しているViewファイルです。
lang
1<!-- File: /app/View/Posts/add.ctp --> 2 3<h1>Add Post</h1> 4<?php 5echo $this->Form->create('Post'); 6echo $this->Form->input('title'); 7echo $this->Form->input('body', array('rows' => '3')); 8echo $this->Form->end('Save Post'); 9?> 10
lang
1<!-- File: /app/View/Posts/view.ctp --> 2 3<h1><?php echo h($post['Post']['title']); ?></h1> 4<p><small>Created: <?php echo $post['Post']['created']; ?></small></p> 5<p><?php echo h($post['Post']['body']); ?></p> 6
lang
1<!-- File: /app/View/Posts/edit.ctp --> 2 3<h1>Edit Post</h1> 4<?php 5echo $this->Form->create('Post'); 6echo $this->Form->input('title'); 7echo $this->Form->input('body', array('rows' => '3')); 8echo $this->Form->input('id', array('type' => 'hidden')); 9echo $this->Form->end('Save Post'); 10?> 11
こちらにレスポンスヘッダ情報に関して追記させて頂きます。(Chromeを使用)
![イメージ説明]WIDTH:600
続き
![イメージ説明]WIDTH:600
追記7/2(addアクション後のソースコードの状態)
![イメージ説明]WIDTH:600