CakePHP 2.4 redirect() with POST data?

cakephp, cakephp-2.4, php, post, redirect

Solution

A little bit old but still no answer accepted so... The answer is no, and yes.

No, there is no direct method since you cannot pass POSTed data using `redirect()` function. You could use `requestAction()`, since you can pass data as posted (see requestAction() here for version cakePHP>=2.0). In that case you pass an url and then an array having the key data with the posted data, something like `$this->requestAction($url, array('data' =>$this->data));` or if you prefer `$this->requestAction($url, array('data' =>$this->request->data));` The problem with `requestAction()` is that the result is environmentally as if you were generating the page of the requested action in the current controller, not in the target, resulting in not very satisfactory effects (at least not usually for me with components behaving not very nicely), so still, no.

...but Yes, you can do something very similar using the `Session` component. This is how I usually do it. The flow would be something like this: View A=>through `postLink()` to Action in A controller=> =>A controller `request->data` to Session variable=> =>action in B controller through `redirect()`=> =>set B controller `request->data` from Session variable=> =>process data in B controller action=> View B

So, in your A controller, let's say in the `sentToNewPage()` action you would have something like

//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);
}

and in 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);
  //...
}

Problem

I'm looking to send the user to another page via a controller method. The other page expects POST data. Normally, the page is accessed with a postLink(). Is there a way to use this in the controller, perhaps with redirect()?

Original source

Related problems