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