@@ -429,43 +429,30 @@ A great way to see the core functionality in action is to look in the
429
429
Redirecting
430
430
~~~~~~~~~~~
431
431
432
- If you want to redirect the user to another page, use the
433
- :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::redirect `
434
- method::
432
+ If you want to redirect the user to another page, use the ``redirectToRoute() `` method::
435
433
436
434
public function indexAction()
437
435
{
438
- return $this->redirect($this->generateUrl( 'homepage') );
436
+ return $this->redirectToRoute( 'homepage');
439
437
}
440
438
441
- The ``generateUrl() `` method is just a helper function that generates the URL
442
- for a given route. For more information, see the :doc: `Routing </book/routing >`
443
- chapter.
444
-
445
- By default, the ``redirect() `` method performs a 302 (temporary) redirect. To
439
+ By default, the ``redirectToRoute() `` method performs a 302 (temporary) redirect. To
446
440
perform a 301 (permanent) redirect, modify the second argument::
447
441
448
442
public function indexAction()
449
443
{
450
- return $this->redirect($this->generateUrl( 'homepage') , 301);
444
+ return $this->redirectToRoute( 'homepage', 301);
451
445
}
452
446
453
447
.. tip ::
454
448
455
- The ``redirect () `` method is simply a shortcut that creates a ``Response ``
449
+ The ``redirectToRoute () `` method is simply a shortcut that creates a ``Response ``
456
450
object that specializes in redirecting the user. It's equivalent to::
457
451
458
452
use Symfony\Component\HttpFoundation\RedirectResponse;
459
453
460
454
return new RedirectResponse($this->generateUrl('homepage'));
461
455
462
- .. versionadded :: 2.6
463
- You can also directly use
464
- :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller::redirectToRoute `
465
- and give it directly the route name like :
466
-
467
- return $this->redirectToRoute('homepage');
468
-
469
456
.. index ::
470
457
single: Controller; Rendering templates
471
458
@@ -635,7 +622,7 @@ For example, imagine you're processing a form submit::
635
622
'Your changes were saved!'
636
623
);
637
624
638
- return $this->redirect($this->generateUrl( ...) );
625
+ return $this->redirectToRoute( ...);
639
626
}
640
627
641
628
return $this->render(...);
0 commit comments