我希望通过 Controller 方法将用户转到另一个页面。另一个页面需要 POST 数据。
通常,使用 postLink() 访问页面。有没有办法在 Controller 中使用它,也许使用 redirect()?
最佳答案
有点旧但仍然没有答案被接受所以......
答案是否定的,是的。
不,没有直接方法,因为您不能使用
redirect()
函数传递 POSTed 数据。
您可以使用requestAction()
,因为您可以按发布的方式传递数据 (see requestAction() here for version cakePHP>=2.0)。
在这种情况下,您传递一个 url,然后传递一个包含关键数据和已发布数据的数组,例如
$this->requestAction($url, array('data' =>$this->data));
或者如果你喜欢$this->requestAction($url, array('data' =>$this->request->data));
requestAction()
的问题在于,结果是环境化的,就好像您是在当前 Controller 中而不是在目标中生成请求操作的页面一样,导致效果不是很令人满意(至少不是通常对我来说组件表现不是很好),所以仍然没有。...但是是的,您可以使用
Session
组件做一些非常相似的事情。
我通常就是这样做的。流程将是这样的:
View A=>通过postLink()
到A controller中的Action=>
=>A controllerrequest->data
到Session变量=>
=>通过redirect()
在B Controller 中执行操作=>
=>从Session变量设置B Controllerrequest->data
=>
=>在 B controller action 处理数据=> View B
因此,在您的 A Controller 中,假设在 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);
}
在 B Controller 中:
//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);
//...
}
关于php - CakePHP 2.4 redirect() 与 POST 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22697712/