1-33
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Symfony2 – Franz Jordán 2011<br />
Redirecting<br />
If you want to redirect the user to another page, use the redirect() method:<br />
public function indexAction()<br />
{<br />
return $this->redirect($this->generateUrl('homepage'));<br />
}<br />
The generateUrl() method is just a helper function that generates the URL for a given route.<br />
For more information, see the Routing chapter.<br />
By default, the redirect() method performs a 302 (temporary) redirect. To perform a 301<br />
(permanent) redirect, modify the second argument:<br />
public function indexAction()<br />
{<br />
}<br />
return $this->redirect($this->generateUrl('homepage'), 301);<br />
The redirect() method is simply a shortcut that creates a Response object that specializes<br />
in redirecting the user. It's equivalent to:<br />
use Symfony\Component\HttpFoundation\RedirectResponse;<br />
return new RedirectResponse($this->generateUrl('homepage'));<br />
Forwarding<br />
You can also easily forward to another controller internally with the forward() method. Instead<br />
of redirecting the user's browser, it makes an internal sub-request, and calls the specified<br />
controller. The forward() method returns the Response object that's returned from that<br />
controller:<br />
public function indexAction($name)<br />
{<br />
$response = $this->forward('AcmeHelloBundle:Hello:fancy', array(<br />
'name' => $name,<br />
'color' => 'green'<br />
));<br />
// further modify the response or return it directly<br />
}<br />
return $response;<br />
Notice that the forward() method uses the same string representation of the controller used in<br />
the routing configuration. In this case, the target controller class will<br />
be HelloController inside someAcmeHelloBundle. The array passed to the method<br />
becomes the arguments on the resulting controller. This same interface is used when<br />
embedding controllers into templates (see Embedding Controllers). The target controller method<br />
should look something like the following:<br />
public function fancyAction($name, $color)<br />
{<br />
// ... create and return a Response object<br />
50