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?
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.
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.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With