Redirect from embedded controller
I use embedded controller to render form that's used on multiple pages:
Twig
{% render 'Bundle:Controller:someForm' %}
Controller
public function someFormAction()
{
// Some logic
...
if ($form->isValid()) {
...
$this->get('session')->setFlash('successful', "Woey!");
return $this->redirect($this->generateUrl('homepage'));
}
return $this->render('Bund开发者_如何学Pythonle:Template:form.html.twig', array('form' => $form->createView()));
}
I need to redirect back to homepage after the form was successfully submitted as a part of post-redirect-get design pattern. If I use it as I described above, I'll get exception as response from embedded controller was 302 instead of 200 (at least I expect it works like this).
Is it possible to redirect normally in such scenario? Or am I approaching the situation (with form that's rendered on multiple pages) from totally wrong angle?
Just to make it official answer, Fabien doesn't think Symfony2 will ever support this feature.
Maybe this will help you. I use this to show 404 pages if a resource was not found in an embedded controller.
try
{
return $this->render('MyBundle:Table:list.html.twig', $data);
}
catch(\Twig_Error_Runtime $e)
{
if($e->getPrevious() instanceof NotFoundHttpException)
{
throw $this->createNotFoundException();
}
else throw $e;
}
You could make a RedirectHttpException
, which would hold your redirection data, and throw it in your embedded controller. Then redirect in your parent controller.
精彩评论