CakePHP重定向方法不起作用?

问题描述 投票:0回答:4

我们开始关注cakephp.org网站上的CakePHP博客教程 - http://book.cakephp.org/2.0/en/tutorials-and-examples/blog/part-two.html

此时我们在提交表单(即函数编辑/添加)后仍然停留在重定向上。这是我们的代码的样子:

public function edit($id = null) {
    $this->Post->id = $id;

    if ($this->request->is('get')) {
        $this->request->data = $this->Post->read();
    } else {
        if ($this->Post->save($this->request->data)) {
            $this->Session->setFlash('Your post has been updated.');
            $this->redirect($this->referer());
        } else {
            $this->Session->setFlash('Unable to update your post.');
        }
    }
}

评论线$this->redirect($this->referer());后,该页面指的是他自己的......添加了一行,它将保留在一个空的白页上。

示例:http://www.drukwerkprijsvergelijk.nl/posts/

请帮助这只小猫,我们绝望了。

php mysql cakephp redirect
4个回答
4
投票

你不能在编辑时使用referer()。那是因为在第一次POST后,引用者就是你现在所在的页面。如果此页面上没有表单帖子,则referer()只能用于重定向(例如删除或访问页面后立即编辑/添加)。但即使使用delete(),你也要小心。来自“视图”会导致重定向进入重定向循环...

您可以将表单中的引用存储为隐藏字段,并使用此方法重定向回来。


2
投票

由于上面解释的原因,您不能在编辑和添加时使用referer()。

使用类似的东西

$this->redirect(array('action' => 'view', $id));

要么

$this->redirect(array('action' => 'index'));

代替。

您还可以尝试在url数组中指定控制器:

$this->redirect(array('controller' => 'posts', 'action' => 'index'));

2
投票

你的PostController或AppController中的PHP代码之外是否有任何空格?我只是查看了编辑页面的源代码,它似乎包含一个空格字符。这可能会阻止设置标头,从而阻止重定向工作。


1
投票

在过滤功能之前使用此功能

    header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); // // HTTP/1.1
    header("Pragma: no-cache");
© www.soinside.com 2019 - 2024. All rights reserved.