我想重定向和设置Flash消息

时间:2015-06-29 16:43:42

标签: cakephp-2.6

我一遍又一遍地尝试了这个CakePHP博客教程(http://book.cakephp.org/2.0/en/getting-started.html)。

但是在添加到帖子时,我的代码无法重定向,也无法使用setFlash消息。

你能告诉我我的代码有什么问题以及如何解决这个问题吗?

我现在认为MAMP设置有问题,因为在刷新索引页面时添加进程是有效的。

  • 的Mac
  • 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.'));
        }
    }
}
?>

0 个答案:

没有答案
相关问题