Redirecting embedded controllers in Symfony2
Symfony2 does not and likely will not ever support redirects from embedded controllers (eg: sub-requests) - https://github.com/symfony/symfony/issues/2517
If you need to perform a redirect from an embedded controller in your Symfony application you can do a forward instead: http://symfony.com/doc/2.0/book/controller.html#forwarding
public function indexAction($name)
{
$response = $this->forward(
'AcmeHelloBundle:Hello:fancy',
array(
'name' => $name,
'color' => 'green',
)
);
return $response;
}
The downside to this is that your URI may no longer be relevant to the content on the page because a forward returns the response of a different controller action to the action of the initial request. It's a necessary trade off unfortunately because Symfony doesn't handle redirects in embedded controller actions.
Written by Steffan Harries
Related protips
4 Responses
Hi Steffan. I don't know if you have tested your code, but forwarding from an embedded controller you get all rendered html inside your embedded controller. Conclusion: It does not work.
I've definitely tested this, I'm working an application right now that makes heavy use of this. PS. I forgot to add that this was with Symfony2.1
Hi Steffan, I tried your code very excited that I may have found something that works. Nope, it renders the content of the embedded controller inside the html. Are you doing anything different?
Not sure what you're doing but if I understand what you're saying but this is how I'm using this:
1 (parent) request for the page: this contains the overall page layout.
1 sub request for the page: this contains the page content.
This means that when I do a forward the I am forwarding the sub request (not the parent).
I think what you and davidpv are looking for as a method to forward the parent request, that's not what this is for I'm afraid.