CakePHP 2.4 redirect() with POST data?

16,138

Solution 1

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

Solution 2

Best solution would be use javascript to redirect.

But if you want more cake I give you some tools CakeAPI: requestAction - it allow to execute controller method of desire with parameters, if you pass 'return', it will return full view output for that action.

//very useful in views
$result =  $this->requestAction('Controller/method', 
       array('return','user_id'=>$userId)
); 

parameter will be accessible in controller via request param

$this->request->params['user_id']
Share:
16,138
StringsOnFire
Author by

StringsOnFire

Not a developer by trade, but I enjoy developing my own side projects. By day, I manage Products. I learn from examples, so please excuse some of my questions... I'll often need to see something work before I can use it myself.

Updated on June 05, 2022

Comments

  • StringsOnFire
    StringsOnFire almost 2 years

    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()?