Str*_*ire 7 php post redirect cakephp cakephp-2.4
我希望通过控制器方法将用户发送到另一个页面.另一页需要POST数据.
通常,使用postLink()访问页面.有没有办法在控制器中使用它,也许使用redirect()?
有点旧,但仍然没有接受答案,所以......答案是否定的,是的。
不,没有直接的方法,因为您无法使用redirect()
函数传递 POSTed 数据。
您可以使用requestAction()
,因为您可以传递已发布的数据(请参阅 requestAction() 此处了解版本 cakePHP>=2.0)。
在这种情况下,您传递一个 url,然后传递一个包含关键数据和已发布数据的数组,例如
$this->requestAction($url, array('data' =>$this->data));
或 如果您愿意,$this->requestAction($url, array('data' =>$this->request->data));
问题requestAction()
是结果是环境性的,就像您在当前控制器中生成请求操作的页面一样,不在目标中,导致效果不是很令人满意(至少对我来说通常是因为组件表现不太好),所以仍然不行。
...但是,是的,您可以使用该Session
组件做一些非常类似的事情。
我通常就是这样做的。流程如下:
查看 A=>postLink()
到 A 控制器中的操作 =>
=> A 控制器request->data
到会话变量 =>
=> B 控制器中的操作通过redirect()
=>
=> 从会话变量设置 B 控制器request->data
=>
=>在 B 控制器操作中处理数据=> 视图 B
所以,在你的 A 控制器中,假设在sentToNewPage()
动作中你会有类似的东西
//Action in A controller
public function sentToNewPage()
{
$this->Session->write('previousPageInfo', $this->request->data);
$url = array('plugin' => 'your_plugin', 'controller'=>'B',
'action'=>'processFromPreviousPage');
$this->redirect($url);
}
Run Code Online (Sandbox Code Playgroud)
在 B 控制器中:
//Action in B controller
public function beforeFilter()
{//not completelly necessary but handy. You can recover directly the data
//from session in the action
if($this->Session->check('previousPageInfo'))
{$this->data = $this->Session->read('previousPageInfo')};
parent::beforeFilter();
}
public function processFromPreviousPage()
{
//do what ever you want to do. Data will be in $this->data or
// if you like it better in $this->request->data
$this->processUserData($this->request->data);
//...
}
Run Code Online (Sandbox Code Playgroud)