@@ -1292,7 +1292,8 @@ in the main article about Symfony templates.
1292
1292
Redirecting to URLs and Routes Directly from a Route
1293
1293
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1294
1294
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:
1296
1297
1297
1298
.. configuration-block ::
1298
1299
@@ -1301,28 +1302,27 @@ Use the ``RedirectController`` to redirect to other routes and URLs:
1301
1302
# config/routes.yaml
1302
1303
doc_shortcut :
1303
1304
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
1305
1317
defaults :
1306
- route : ' doc_page'
1307
- # optionally you can define some arguments passed to the route
1308
1318
page : ' index'
1309
1319
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
1318
1320
1319
1321
legacy_doc :
1320
1322
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
1326
1326
1327
1327
.. code-block :: xml
1328
1328
@@ -1333,68 +1333,71 @@ Use the ``RedirectController`` to redirect to other routes and URLs:
1333
1333
xsi : schemaLocation =" http://symfony.com/schema/routing
1334
1334
https://symfony.com/schema/routing/routing-1.0.xsd" >
1335
1335
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">
1339
1340
<!-- optionally you can define some arguments passed to the route -->
1340
1341
<default key =" page" >index</default >
1341
1342
<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 >
1344
1343
<!-- add this to keep the original query string parameters when redirecting -->
1345
1344
<default key =" keepQueryParams" >true</default >
1346
1345
<!-- add this to keep the HTTP method when redirecting. The redirect status changes:
1347
1346
* for temporary redirects, it uses the 307 status code instead of 302
1348
1347
* for permanent redirects, it uses the 308 status code instead of 301 -->
1349
1348
<default key =" keepRequestMethod" >true</default >
1350
- </route >
1349
+ </redirect- route >
1351
1350
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"
1354
1352
<!-- 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"
1356
1354
<!-- 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 >
1359
1357
</routes >
1360
1358
1361
1359
.. code-block :: php
1362
1360
1363
1361
// config/routes.php
1364
1362
use App\Controller\DefaultController;
1365
- use Symfony\Bundle\FrameworkBundle\Controller\RedirectController;
1366
1363
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
1367
1364
1368
1365
return function (RoutingConfigurator $routes) {
1369
1366
$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([
1374
1382
'page' => 'index',
1375
1383
'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,
1384
1384
])
1385
1385
;
1386
1386
1387
1387
$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()
1395
1393
;
1396
1394
};
1397
1395
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
+
1398
1401
.. tip ::
1399
1402
1400
1403
Symfony also provides some utilities to
0 commit comments