【问题标题】:Cakephp Blog Tutorial - Add RoutineCakephp 博客教程 - 添加例程
【发布时间】:2013-12-14 03:42:10
【问题描述】:

我按照 cakePHP 博客教程进行操作,但在 cakephp/posts/add 例程中发生了逻辑错误

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

我从教程中得到了默认的cakephp/posts/add 例程工作得很好,但是当我复制例程并尝试将其重命名为cakephp/apples/add(发给苹果的帖子)时,$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.'));
    }
}   

这可能是什么问题?

【问题讨论】:

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


    【解决方案1】:

    问题出在这一行:

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

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多