@@ -1308,15 +1308,18 @@ A possible solution is to change the parameter requirements to be more permissiv
1308
1308
Route Aliasing
1309
1309
--------------
1310
1310
1311
- Route alias allow you to have multiple name for the same route:
1311
+ Route alias allows you to have multiple names for the same route
1312
+ and can be used to provide backward compatibility for routes that
1313
+ have been renamed. Let's say you have a route called ``product_show ``:
1312
1314
1313
1315
.. configuration-block ::
1314
1316
1315
1317
.. code-block :: yaml
1316
1318
1317
1319
# config/routes.yaml
1318
- new_route_name :
1319
- alias : original_route_name
1320
+ product_show :
1321
+ path : /product/{id}
1322
+ controller : App\Controller\ProductController::show
1320
1323
1321
1324
.. code-block :: xml
1322
1325
@@ -1327,7 +1330,7 @@ Route alias allow you to have multiple name for the same route:
1327
1330
xsi : schemaLocation =" http://symfony.com/schema/routing
1328
1331
https://symfony.com/schema/routing/routing-1.0.xsd" >
1329
1332
1330
- <route id =" new_route_name " alias = " original_route_name " />
1333
+ <route id =" product_show " path = " /product/{id} " controller = " App\Controller\ProductController::show " />
1331
1334
</routes >
1332
1335
1333
1336
.. code-block :: php
@@ -1336,38 +1339,101 @@ Route alias allow you to have multiple name for the same route:
1336
1339
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
1337
1340
1338
1341
return static function (RoutingConfigurator $routes): void {
1339
- $routes->alias('new_route_name', 'original_route_name');
1342
+ $routes->add('product_show', '/product/{id}')
1343
+ ->controller('App\Controller\ProductController::show');
1340
1344
};
1341
1345
1342
- In this example, both ``original_route_name `` and ``new_route_name `` routes can
1346
+ Now, let's say you want to create a new route called ``product_details ``
1347
+ that acts exactly the same as ``product_show ``.
1348
+
1349
+ Instead of duplicating the original route, you can create an alias for it.
1350
+
1351
+ .. configuration-block ::
1352
+
1353
+ .. code-block :: yaml
1354
+
1355
+ # config/routes.yaml
1356
+ product_show :
1357
+ path : /product/{id}
1358
+ controller : App\Controller\ProductController::show
1359
+
1360
+ product_details :
1361
+ # "alias" option refers to the name of the route declared above
1362
+ alias : product_show
1363
+
1364
+ .. code-block :: xml
1365
+
1366
+ <!-- config/routes.xml -->
1367
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
1368
+ <routes xmlns =" http://symfony.com/schema/routing"
1369
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
1370
+ xsi : schemaLocation =" http://symfony.com/schema/routing
1371
+ https://symfony.com/schema/routing/routing-1.0.xsd" >
1372
+
1373
+ <route id =" product_show" path =" /product/{id}" controller =" App\Controller\ProductController::show" />
1374
+ <!-- "alias" attribute value refers to the name of the route declared above -->
1375
+ <route id =" product_details" alias =" product_show" />
1376
+ </routes >
1377
+
1378
+ .. code-block :: php
1379
+
1380
+ // config/routes.php
1381
+ use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
1382
+
1383
+ return static function (RoutingConfigurator $routes): void {
1384
+ $routes->add('product_show', '/product/{id}')
1385
+ ->controller('App\Controller\ProductController::show');
1386
+ // second argument refers to the name of the route declared above
1387
+ $routes->alias('product_details', 'product_show');
1388
+ };
1389
+
1390
+ In this example, both ``product_show `` and ``product_details `` routes can
1343
1391
be used in the application and will produce the same result.
1344
1392
1345
1393
.. _routing-alias-deprecation :
1346
1394
1347
1395
Deprecating Route Aliases
1348
1396
~~~~~~~~~~~~~~~~~~~~~~~~~
1349
1397
1350
- If some route alias should no longer be used (because it is outdated or
1351
- you decided not to maintain it anymore), you can deprecate its definition:
1398
+ Route aliases can be used to provide backward compatibility for routes that
1399
+ have been renamed.
1400
+
1401
+ Now, let's say you want to replace the ``product_show `` route in favor of
1402
+ ``product_details `` and mark the old one as deprecated.
1403
+
1404
+ In the previous example, the alias ``product_details `` was pointing to
1405
+ ``product_show `` route.
1406
+
1407
+ To mark the ``product_show `` route as deprecated, you need to "switch" the alias.
1408
+ The ``product_show `` become the alias, and will now point to the ``product_details `` route.
1409
+ This way, the ``product_show `` alias could be deprecated.
1352
1410
1353
1411
.. configuration-block ::
1354
1412
1355
1413
.. code-block :: yaml
1356
1414
1357
- new_route_name :
1358
- alias : original_route_name
1415
+ # Move the concrete route definition under ``product_details``
1416
+ product_details :
1417
+ path : /product/{id}
1418
+ controller : App\Controller\ProductController::show
1419
+
1420
+ # Define the alias and the deprecation under the ``product_show`` definition
1421
+ product_show :
1422
+ alias : product_details
1359
1423
1360
1424
# this outputs the following generic deprecation message:
1361
- # Since acme/package 1.2: The "new_route_name " route alias is deprecated. You should stop using it, as it will be removed in the future.
1425
+ # Since acme/package 1.2: The "product_show " route alias is deprecated. You should stop using it, as it will be removed in the future.
1362
1426
deprecated :
1363
1427
package : ' acme/package'
1364
1428
version : ' 1.2'
1365
1429
1366
- # you can also define a custom deprecation message (%alias_id% placeholder is available)
1430
+ # or
1431
+
1432
+ # you can define a custom deprecation message (%alias_id% placeholder is available)
1367
1433
deprecated :
1368
1434
package : ' acme/package'
1369
1435
version : ' 1.2'
1370
- message : ' The "%alias_id%" route alias is deprecated. Do not use it anymore .'
1436
+ message : ' The "%alias_id%" route alias is deprecated. Please use "product_details" instead .'
1371
1437
1372
1438
.. code-block :: xml
1373
1439
@@ -1377,35 +1443,46 @@ you decided not to maintain it anymore), you can deprecate its definition:
1377
1443
xsi : schemaLocation =" http://symfony.com/schema/routing
1378
1444
https://symfony.com/schema/routing/routing-1.0.xsd" >
1379
1445
1380
- <route id =" new_route_name" alias =" original_route_name" >
1446
+ <!-- Move the concrete route definition under ``product_details`` -->
1447
+ <route id =" product_details" path =" /product/{id}" controller =" App\Controller\ProductController::show" />
1448
+
1449
+ <!-- Define the alias and the deprecation under the ``product_show`` definition -->
1450
+ <route id =" product_show" alias =" product_details" >
1381
1451
<!-- this outputs the following generic deprecation message:
1382
- Since acme/package 1.2: The "new_route_name " route alias is deprecated. You should stop using it, as it will be removed in the future. -->
1452
+ Since acme/package 1.2: The "product_show " route alias is deprecated. You should stop using it, as it will be removed in the future. -->
1383
1453
<deprecated package =" acme/package" version =" 1.2" />
1384
1454
1385
- <!-- you can also define a custom deprecation message (%alias_id% placeholder is available) -->
1455
+ <!-- or -->
1456
+
1457
+ <!-- you can define a custom deprecation message (%alias_id% placeholder is available) -->
1386
1458
<deprecated package =" acme/package" version =" 1.2" >
1387
- The "%alias_id%" route alias is deprecated. Do not use it anymore .
1459
+ The "%alias_id%" route alias is deprecated. Please use "product_details" instead .
1388
1460
</deprecated >
1389
1461
</route >
1390
1462
</routes >
1391
1463
1392
1464
.. code-block :: php
1393
1465
1394
- $routes->alias('new_route_name', 'original_route_name')
1466
+ $routes->add('product_details', '/product/{id}')
1467
+ ->controller('App\Controller\ProductController::show');
1468
+
1469
+ $routes->alias('product_show', 'product_details')
1395
1470
// this outputs the following generic deprecation message:
1396
- // Since acme/package 1.2: The "new_route_name " route alias is deprecated. You should stop using it, as it will be removed in the future.
1471
+ // Since acme/package 1.2: The "product_show " route alias is deprecated. You should stop using it, as it will be removed in the future.
1397
1472
->deprecate('acme/package', '1.2', '')
1398
1473
1399
- // you can also define a custom deprecation message (%alias_id% placeholder is available)
1474
+ // or
1475
+
1476
+ // you can define a custom deprecation message (%alias_id% placeholder is available)
1400
1477
->deprecate(
1401
1478
'acme/package',
1402
1479
'1.2',
1403
- 'The "%alias_id%" route alias is deprecated. Do not use it anymore .'
1480
+ 'The "%alias_id%" route alias is deprecated. Please use "product_details" instead .'
1404
1481
)
1405
1482
;
1406
1483
1407
- In this example, every time the ``new_route_name `` alias is used, a deprecation
1408
- warning is triggered, advising you to stop using that alias .
1484
+ In this example, every time the ``product_show `` alias is used, a deprecation
1485
+ warning is triggered, advising you to stop using this route and prefer using `` product_details `` .
1409
1486
1410
1487
The message is actually a message template, which replaces occurrences of the
1411
1488
``%alias_id% `` placeholder by the route alias name. You **must ** have
0 commit comments