Skip to content

Commit a9e3f8b

Browse files
committed
fix(route-alias): Clarify route aliases examples with explicit route names
1 parent d232ccc commit a9e3f8b

File tree

1 file changed

+93
-17
lines changed

1 file changed

+93
-17
lines changed

Diff for: routing.rst

+93-17
Original file line numberDiff line numberDiff line change
@@ -1338,13 +1338,56 @@ Route Aliasing
13381338

13391339
Route alias allow you to have multiple name for the same route:
13401340

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+
13411379
.. configuration-block::
13421380

13431381
.. code-block:: yaml
13441382
13451383
# config/routes.yaml
1384+
some_route_name:
1385+
path: /some-path
1386+
controller: App\Controller\SomeController::index
1387+
13461388
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
13481391
13491392
.. code-block:: xml
13501393
@@ -1355,7 +1398,9 @@ Route alias allow you to have multiple name for the same route:
13551398
xsi:schemaLocation="http://symfony.com/schema/routing
13561399
https://symfony.com/schema/routing/routing-1.0.xsd">
13571400
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"/>
13591404
</routes>
13601405
13611406
.. code-block:: php
@@ -1364,38 +1409,58 @@ Route alias allow you to have multiple name for the same route:
13641409
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
13651410
13661411
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');
13681416
};
13691417
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
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 ``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:
13801437

13811438
.. configuration-block::
13821439

13831440
.. code-block:: yaml
13841441
1442+
# Move the concrete route definition under ``new_route_name``
13851443
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
13871450
13881451
# 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.
13901453
deprecated:
13911454
package: 'acme/package'
13921455
version: '1.2'
13931456
1457+
# or
1458+
13941459
# you can also define a custom deprecation message (%alias_id% placeholder is available)
13951460
deprecated:
13961461
package: 'acme/package'
13971462
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.'
13991464
14001465
.. code-block:: xml
14011466
@@ -1405,35 +1470,46 @@ you decided not to maintain it anymore), you can deprecate its definition:
14051470
xsi:schemaLocation="http://symfony.com/schema/routing
14061471
https://symfony.com/schema/routing/routing-1.0.xsd">
14071472
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">
14091478
<!-- 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. -->
14111480
<deprecated package="acme/package" version="1.2"/>
14121481
1482+
<!-- or -->
1483+
14131484
<!-- you can also define a custom deprecation message (%alias_id% placeholder is available) -->
14141485
<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.
14161487
</deprecated>
14171488
</route>
14181489
</routes>
14191490
14201491
.. code-block:: php
14211492
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')
14231497
// 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.
14251499
->deprecate('acme/package', '1.2', '')
14261500
1501+
// or
1502+
14271503
// you can also define a custom deprecation message (%alias_id% placeholder is available)
14281504
->deprecate(
14291505
'acme/package',
14301506
'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.'
14321508
)
14331509
;
14341510
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``.
14371513

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

0 commit comments

Comments
 (0)