Redirecting¶
If you want to redirect the user to another page, use the
redirectToRoute()
method:1 2 3 4 5 6 7 | public function indexAction()
{
return $this->redirectToRoute('homepage');
// redirectToRoute is equivalent to using redirect() and generateUrl() together:
// return $this->redirect($this->generateUrl('homepage'));
}
|
By default, the
redirectToRoute()
method performs a 302 (temporary) redirect. To perform a 301 (permanent) redirect, modify the third argument:public function indexAction()
{
return $this->redirectToRoute('homepage', array(), 301);
}
To redirect to an external site, use
redirect()
and pass it the external URL:public function indexAction()
{
return $this->redirect('http://symfony.com/doc');
}
For more information, see the Routing chapter.
The
redirectToRoute()
method is simply a shortcut that creates a Response
object that specializes in redirecting the user. It's equivalent to:1 2 3 4 5 6 | use Symfony\Component\HttpFoundation\RedirectResponse;
public function indexAction()
{
return new RedirectResponse($this->generateUrl('homepage'));
}
|
No comments:
Post a Comment