我们开始遵循CakePHP博客教程,该教程托管在cakehp.org网站上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/
请帮帮这些小猫,我们走投无路了。
您不能在编辑时使用referr()。这是因为在第一次POST之后,referer与您现在所在的页面相同。referr()只能用于重定向,如果此页面上没有表单帖子(例如删除或访问页面后立即编辑/添加)。但即使使用delete(),也必须小心。来自"视图"将导致重定向运行到重定向循环中。。。
您可以将referer作为隐藏字段存储在表单中,并使用它重定向回。
由于上面解释的原因,您不能在编辑和添加时使用referr()。
使用类似的东西
$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");