Skip to content

Commit a28b614

Browse files
welcoMatticOskarStark
authored andcommitted
[Routing] Clarify route aliases examples with explicit route names
1 parent d232ccc commit a28b614

File tree

1 file changed

+100
-23
lines changed

1 file changed

+100
-23
lines changed

routing.rst

Lines changed: 100 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,15 +1336,18 @@ A possible solution is to change the parameter requirements to be more permissiv
13361336
Route Aliasing
13371337
--------------
13381338

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``:
13401342

13411343
.. configuration-block::
13421344

13431345
.. code-block:: yaml
13441346
13451347
# 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
13481351
13491352
.. code-block:: xml
13501353
@@ -1355,7 +1358,7 @@ Route alias allow you to have multiple name for the same route:
13551358
xsi:schemaLocation="http://symfony.com/schema/routing
13561359
https://symfony.com/schema/routing/routing-1.0.xsd">
13571360
1358-
<route id="new_route_name" alias="original_route_name"/>
1361+
<route id="product_show" path="/product/{id}" controller="App\Controller\ProductController::show"/>
13591362
</routes>
13601363
13611364
.. code-block:: php
@@ -1364,38 +1367,101 @@ Route alias allow you to have multiple name for the same route:
13641367
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
13651368
13661369
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');
13681372
};
13691373
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
13711419
be used in the application and will produce the same result.
13721420

13731421
.. _routing-alias-deprecation:
13741422

13751423
Deprecating Route Aliases
13761424
~~~~~~~~~~~~~~~~~~~~~~~~~
13771425

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.
13801438

13811439
.. configuration-block::
13821440

13831441
.. code-block:: yaml
13841442
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
13871451
13881452
# 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.
13901454
deprecated:
13911455
package: 'acme/package'
13921456
version: '1.2'
13931457
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)
13951461
deprecated:
13961462
package: 'acme/package'
13971463
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.'
13991465
14001466
.. code-block:: xml
14011467
@@ -1405,35 +1471,46 @@ you decided not to maintain it anymore), you can deprecate its definition:
14051471
xsi:schemaLocation="http://symfony.com/schema/routing
14061472
https://symfony.com/schema/routing/routing-1.0.xsd">
14071473
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">
14091479
<!-- 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. -->
14111481
<deprecated package="acme/package" version="1.2"/>
14121482
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) -->
14141486
<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.
14161488
</deprecated>
14171489
</route>
14181490
</routes>
14191491
14201492
.. code-block:: php
14211493
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')
14231498
// 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.
14251500
->deprecate('acme/package', '1.2', '')
14261501
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)
14281505
->deprecate(
14291506
'acme/package',
14301507
'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.'
14321509
)
14331510
;
14341511
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``.
14371514

14381515
The message is actually a message template, which replaces occurrences of the
14391516
``%alias_id%`` placeholder by the route alias name. You **must** have

0 commit comments

Comments
 (0)