Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2 redirect using POST

In Symfony 2 I have the following code in my Controller:

// prepare to render the seller info panel
$response = array(
    'data' => $data,
);

// render the seller info panel
return $this->redirect($this->generateUrl('route', $response));

where route is:

route:
    pattern:  /listing/complete/{data}
    defaults: { _controller: FooBundle:Foo:action }
    requirements:
        _method:  POST

This doesn't work since the redirect is making a GET request. I've also tried it this pattern, but its not matching the route:

route:
    pattern:  /listing/complete
    defaults: { _controller: FooBundle:Foo:action }
    requirements:
        _method:  POST

I've found the routing documentation unhelpful. Is there a way that I can have the redirect make a POST request? What would the route look like, and do I have to do anything in the controller to make it happen?

like image 271
ContextSwitch Avatar asked Jun 27 '12 14:06

ContextSwitch


3 Answers

Latest way of doing POST request redirect (as of Symfony 2.6) is simply:

return $this->redirectToRoute('route', [
    'request' => $request
], 307);

Code 307 preserves the request method, while redirectToRoute() is a shortcut method.

like image 153
Damaged Organic Avatar answered Oct 20 '22 21:10

Damaged Organic


It's impossible to redirect a POST request because the browser would have to re-send the POST data (which it doesn't). What you should do instead in this case is use forwarding.

like image 37
chiborg Avatar answered Oct 20 '22 22:10

chiborg


I had the same error with you when I used $this->generateUrl with passed parameters. However, my redirect worked when I tried this:

$this->get('router')->generate('route_name', array('param1' => 'paramVal'))

(I know it would not help you that much right now.)

like image 1
Floricel Avatar answered Oct 20 '22 21:10

Floricel