Redirect to new page w/ POST data (PHP/Zend)

angrychimp picture angrychimp · Aug 21, 2009 · Viewed 34.9k times · Source

I'm trying to figure out how to redirect to a new page (different application, controller, action) and maintain the current POST data.

My app is validating the POST data and if a certain criteria is met, I want to redirect to a different location, but make sure the POST data is passed as POST data to that new page. Here's a rough example:

POST /app1/example HTTP/1.1
Host: example.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 17
    var1=foo&var2=bar

In my exampleAction (Zend_Controller_Action), I check to see if var1 == foo, and if it does I want to redirect (302) to /app2/example with that same POST data. Something maybe like this?

HTTP/1.x 302 Found
Location: /app2/example
Content-Type: application/x-www-form-urlencoded
Content-Length: 17
    var1=foo&var2=bar

I can see how to create a new HTTP request using Zend_Http_Client, but I'm not looking to simply request the page and display the content. Should I still be looking to use Zend_Http_Client anyway? At the moment I'm playing with stuff like this:

$this->getResponse()->setRedirect('/app2/example/', 302);
$this->getResponse()->setBody(http_build_query($this->_request->getPost()));

I'm sure what I want to do is possible through some means of trickery and demon-cavorting, but it's definitely alluding me. Any help is appreciated.

- rk

Answer

gnarf picture gnarf · Aug 21, 2009

Rather than redirecting the browser to the new location, why not just forward the request from the controller to the other controller?

return $this->_forward("action", "controller", "module");