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