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