繁体   English   中英

CakePHP重定向方法不起作用?

[英]CakePHP Redirect method not working?

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

在这一点上,我们在提交表单(即功能编辑/添加)后仍无法进行重定向。 我们的代码如下所示:

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/

请帮助这只小猫,我们很拼命。

您不能在编辑时使用Referer()。 多数民众赞成是因为在第一次POST之后,引荐来源网址与您现在所在的页面相同。 如果此页面上没有表单发布,则referer()仅可用于重定向(例如,删除或在访问该页面后立即编辑/添加)。 但是即使使用delete(),也必须要小心。 来自“视图”的结果将导致重定向进入重定向循环...

您可以将引荐来源网址存储在表单中作为隐藏字段,并使用此网址重定向回来。

由于上述原因标记,您不能在edit和add上使用Referer()。

使用类似

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

要么

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

代替。

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

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

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

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

    header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); // // HTTP/1.1
    header("Pragma: no-cache");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM