Cakephp博客教程 - 添加例程

时间:2013-12-14 03:42:11

标签: php cakephp controller cakephp-2.0 cakephp-appmodel

我关注了cakePHP博客教程,我在cakephp/posts/add例行程序中发生了逻辑错误

http://book.cakephp.org/2.0/en/tutorials-and-examples/blog/part-two.html

我从教程中得到了默认的cakephp/posts/add例程,但是当我复制例程并尝试将其重命名为cakephp/apples/add(Posts to Apples)时,似乎$this->Apple->save($this->request->data)并且$this->redirect(array('action' => 'index'))无效,它只刷新页面,不会重定向到索引视图,也不会保存记录。

public function add() {
    if ($this->request->is('apple')) {
        $this->Apple->create();
        if ($this->Apple->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.'));
    }
}   

这可能是什么问题?

2 个答案:

答案 0 :(得分:2)

问题出在这一行:

if ($this->request->is('apple')) {

您正在检查请求的类型以确定它是否是表单提交(通常是POST),因此它应该是

if ($this->request->is('post')) {

答案 1 :(得分:0)

Ypu应该处理$this->request->is('post');$this->request->is('get');的请求,如果你想在默认控制器中使用不同的模型,比如在PostsController中想要使用Apple模型,你需要在控制器中加载这个模型,如$this->loadModel('Apple');或定义$ uses变量,如public $uses = array('Post', 'Apple');

相关问题