我一遍又一遍地尝试了这个 CakePHP 博客教程 ( http://book.cakephp.org/2.0/en/getting-started.html )。
但是我的代码在添加到帖子时无法重定向并且无法使用 setFlash 消息。
你能告诉我我的代码有什么问题以及如何解决这个问题吗?
我现在认为 MAMP 设置有问题,因为在刷新索引页面时添加进程正在工作。
- 苹果电脑
- MAMP
这是我的代码。
<?php
//File: /app/Controller/PostsController.php
class PostsController extends AppController {
public $helpers = array('Html', 'Form', 'Session');
public $components = array('Session');
public function index() {
$this->set('posts', $this->Post->find('all'));
}
public function view($id = null) {
if(!$id) {
throw new NotFoundException(__('Invalid post'));
}
$post = $this->Post->findById($id);
if(!$post) {
throw new NotFoundException(__('Invalid post'));
}
$this->set('post', $post);
}
public function add() {
if($this->request->is('post')) {
$this->Post->create();
if($this->Post->save($this->request->data)) {
$this->Session->setFlash(__('Your post has been saved.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to add your post.'));
}
}
}
?>