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