@@ -1292,7 +1292,8 @@ in the main article about Symfony templates.
12921292Redirecting to URLs and Routes Directly from a Route
12931293~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12941294
1295- Use the ``RedirectController `` to redirect to other routes and URLs:
1295+ Use the :class: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ RedirectController `
1296+ to redirect to other routes and URLs:
12961297
12971298.. configuration-block ::
12981299
@@ -1301,28 +1302,27 @@ Use the ``RedirectController`` to redirect to other routes and URLs:
13011302 # config/routes.yaml
13021303 doc_shortcut :
13031304 path : /doc
1304- controller : Symfony\Bundle\FrameworkBundle\Controller\RedirectController
1305+ redirect_to_route : ' doc_page'
1306+
1307+ # redirections are temporary by default (code 302) but you can make them permanent (code 301)
1308+ permanent : true
1309+ # add this to keep the original query string parameters when redirecting
1310+ keepQueryParams : true
1311+ # add this to keep the HTTP method when redirecting. The redirect status changes
1312+ # * for temporary redirects, it uses the 307 status code instead of 302
1313+ # * for permanent redirects, it uses the 308 status code instead of 301
1314+ keepRequestMethod : true
1315+
1316+ # optionally you can define some arguments passed to the route
13051317 defaults :
1306- route : ' doc_page'
1307- # optionally you can define some arguments passed to the route
13081318 page : ' index'
13091319 version : ' current'
1310- # redirections are temporary by default (code 302) but you can make them permanent (code 301)
1311- permanent : true
1312- # add this to keep the original query string parameters when redirecting
1313- keepQueryParams : true
1314- # add this to keep the HTTP method when redirecting. The redirect status changes
1315- # * for temporary redirects, it uses the 307 status code instead of 302
1316- # * for permanent redirects, it uses the 308 status code instead of 301
1317- keepRequestMethod : true
13181320
13191321 legacy_doc :
13201322 path : /legacy/doc
1321- controller : Symfony\Bundle\FrameworkBundle\Controller\RedirectController
1322- defaults :
1323- # this value can be an absolute path or an absolute URL
1324- path : ' https://legacy.example.com/doc'
1325- permanent : true
1323+ # this value can be an absolute path or an absolute URL
1324+ redirect_to_url : ' https://legacy.example.com/doc'
1325+ permanent : true
13261326
13271327 .. code-block :: xml
13281328
@@ -1333,68 +1333,71 @@ Use the ``RedirectController`` to redirect to other routes and URLs:
13331333 xsi : schemaLocation =" http://symfony.com/schema/routing
13341334 https://symfony.com/schema/routing/routing-1.0.xsd" >
13351335
1336- <route id =" doc_shortcut" path =" /doc"
1337- controller =" Symfony\Bundle\FrameworkBundle\Controller\RedirectController" >
1338- <default key =" route" >doc_page</default >
1336+ <redirect-route id =" doc_shortcut" path =" /doc"
1337+ redirect-to-route =" doc_page"
1338+ <!-- redirections are temporary by default (code 302) but you can make them permanent (code 301)-->
1339+ permanent="true">
13391340 <!-- optionally you can define some arguments passed to the route -->
13401341 <default key =" page" >index</default >
13411342 <default key =" version" >current</default >
1342- <!-- redirections are temporary by default (code 302) but you can make them permanent (code 301)-->
1343- <default key =" permanent" >true</default >
13441343 <!-- add this to keep the original query string parameters when redirecting -->
13451344 <default key =" keepQueryParams" >true</default >
13461345 <!-- add this to keep the HTTP method when redirecting. The redirect status changes:
13471346 * for temporary redirects, it uses the 307 status code instead of 302
13481347 * for permanent redirects, it uses the 308 status code instead of 301 -->
13491348 <default key =" keepRequestMethod" >true</default >
1350- </route >
1349+ </redirect- route >
13511350
1352- <route id =" legacy_doc" path =" /legacy/doc"
1353- controller =" Symfony\Bundle\FrameworkBundle\Controller\RedirectController" >
1351+ <url-redirect-route id =" legacy_doc" path =" /legacy/doc"
13541352 <!-- this value can be an absolute path or an absolute URL -->
1355- < default key = " path " > https://legacy.example.com/doc</ default >
1353+ redirect-to-url=" https://legacy.example.com/doc"
13561354 <!-- redirections are temporary by default (code 302) but you can make them permanent (code 301)-->
1357- < default key = " permanent " > true</ default >
1358- </route >
1355+ permanent=" true" >
1356+ </url-redirect- route >
13591357 </routes >
13601358
13611359 .. code-block :: php
13621360
13631361 // config/routes.php
13641362 use App\Controller\DefaultController;
1365- use Symfony\Bundle\FrameworkBundle\Controller\RedirectController;
13661363 use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
13671364
13681365 return function (RoutingConfigurator $routes) {
13691366 $routes->add('doc_shortcut', '/doc')
1370- ->controller(RedirectController::class)
1371- ->defaults([
1372- 'route' => 'doc_page',
1373- // optionally you can define some arguments passed to the template
1367+ ->redirectToRoute('doc_page')
1368+
1369+ // redirections are temporary by default (code 302) but you can make them permanent (code 301)
1370+ ->permanent()
1371+
1372+ // add this to keep the original query string parameters when redirecting
1373+ ->keepQueryParams()
1374+
1375+ // add this to keep the HTTP method when redirecting. The redirect status changes:
1376+ // * for temporary redirects, it uses the 307 status code instead of 302
1377+ // * for permanent redirects, it uses the 308 status code instead of 301
1378+ ->keepRequestMethod()
1379+
1380+ // optionally you can define some arguments passed to the template
1381+ ->defaults([
13741382 'page' => 'index',
13751383 'version' => 'current',
1376- // redirections are temporary by default (code 302) but you can make them permanent (code 301)
1377- 'permanent' => true,
1378- // add this to keep the original query string parameters when redirecting
1379- 'keepQueryParams' => true,
1380- // add this to keep the HTTP method when redirecting. The redirect status changes:
1381- // * for temporary redirects, it uses the 307 status code instead of 302
1382- // * for permanent redirects, it uses the 308 status code instead of 301
1383- 'keepRequestMethod' => true,
13841384 ])
13851385 ;
13861386
13871387 $routes->add('legacy_doc', '/legacy/doc')
1388- ->controller(RedirectController::class)
1389- ->defaults([
1390- // this value can be an absolute path or an absolute URL
1391- 'path' => 'https://legacy.example.com/doc',
1392- // redirections are temporary by default (code 302) but you can make them permanent (code 301)
1393- 'permanent' => true,
1394- ])
1388+ // this value can be an absolute path or an absolute URL
1389+ ->redirectToUrl('https://legacy.example.com/doc')
1390+
1391+ // redirections are temporary by default (code 302) but you can make them permanent (code 301)
1392+ ->permanent()
13951393 ;
13961394 };
13971395
1396+ .. versionadded :: 5.1
1397+
1398+ This short syntax was introduced in Symfony 5.1. Before you had to
1399+ define the controller and specific route attributes using ``defaults ``.
1400+
13981401.. tip ::
13991402
14001403 Symfony also provides some utilities to
0 commit comments