Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 24aecee

Browse filesBrowse files
committed
Merge branch '6.4' into 7.2
* 6.4: [Routing] Clarify route aliases examples with explicit route names
2 parents 88a7eb2 + 6556d97 commit 24aecee
Copy full SHA for 24aecee

File tree

1 file changed

+100
-23
lines changed
Filter options

1 file changed

+100
-23
lines changed

‎routing.rst

Copy file name to clipboardExpand all lines: routing.rst
+100-23Lines changed: 100 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,15 +1308,18 @@ A possible solution is to change the parameter requirements to be more permissiv
13081308
Route Aliasing
13091309
--------------
13101310

1311-
Route alias allow you to have multiple name for the same route:
1311+
Route alias allows you to have multiple names for the same route
1312+
and can be used to provide backward compatibility for routes that
1313+
have been renamed. Let's say you have a route called ``product_show``:
13121314

13131315
.. configuration-block::
13141316

13151317
.. code-block:: yaml
13161318
13171319
# config/routes.yaml
1318-
new_route_name:
1319-
alias: original_route_name
1320+
product_show:
1321+
path: /product/{id}
1322+
controller: App\Controller\ProductController::show
13201323
13211324
.. code-block:: xml
13221325
@@ -1327,7 +1330,7 @@ Route alias allow you to have multiple name for the same route:
13271330
xsi:schemaLocation="http://symfony.com/schema/routing
13281331
https://symfony.com/schema/routing/routing-1.0.xsd">
13291332
1330-
<route id="new_route_name" alias="original_route_name"/>
1333+
<route id="product_show" path="/product/{id}" controller="App\Controller\ProductController::show"/>
13311334
</routes>
13321335
13331336
.. code-block:: php
@@ -1336,38 +1339,101 @@ Route alias allow you to have multiple name for the same route:
13361339
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
13371340
13381341
return static function (RoutingConfigurator $routes): void {
1339-
$routes->alias('new_route_name', 'original_route_name');
1342+
$routes->add('product_show', '/product/{id}')
1343+
->controller('App\Controller\ProductController::show');
13401344
};
13411345
1342-
In this example, both ``original_route_name`` and ``new_route_name`` routes can
1346+
Now, let's say you want to create a new route called ``product_details``
1347+
that acts exactly the same as ``product_show``.
1348+
1349+
Instead of duplicating the original route, you can create an alias for it.
1350+
1351+
.. configuration-block::
1352+
1353+
.. code-block:: yaml
1354+
1355+
# config/routes.yaml
1356+
product_show:
1357+
path: /product/{id}
1358+
controller: App\Controller\ProductController::show
1359+
1360+
product_details:
1361+
# "alias" option refers to the name of the route declared above
1362+
alias: product_show
1363+
1364+
.. code-block:: xml
1365+
1366+
<!-- config/routes.xml -->
1367+
<?xml version="1.0" encoding="UTF-8" ?>
1368+
<routes xmlns="http://symfony.com/schema/routing"
1369+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1370+
xsi:schemaLocation="http://symfony.com/schema/routing
1371+
https://symfony.com/schema/routing/routing-1.0.xsd">
1372+
1373+
<route id="product_show" path="/product/{id}" controller="App\Controller\ProductController::show"/>
1374+
<!-- "alias" attribute value refers to the name of the route declared above -->
1375+
<route id="product_details" alias="product_show"/>
1376+
</routes>
1377+
1378+
.. code-block:: php
1379+
1380+
// config/routes.php
1381+
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
1382+
1383+
return static function (RoutingConfigurator $routes): void {
1384+
$routes->add('product_show', '/product/{id}')
1385+
->controller('App\Controller\ProductController::show');
1386+
// second argument refers to the name of the route declared above
1387+
$routes->alias('product_details', 'product_show');
1388+
};
1389+
1390+
In this example, both ``product_show`` and ``product_details`` routes can
13431391
be used in the application and will produce the same result.
13441392

13451393
.. _routing-alias-deprecation:
13461394

13471395
Deprecating Route Aliases
13481396
~~~~~~~~~~~~~~~~~~~~~~~~~
13491397

1350-
If some route alias should no longer be used (because it is outdated or
1351-
you decided not to maintain it anymore), you can deprecate its definition:
1398+
Route aliases can be used to provide backward compatibility for routes that
1399+
have been renamed.
1400+
1401+
Now, let's say you want to replace the ``product_show`` route in favor of
1402+
``product_details`` and mark the old one as deprecated.
1403+
1404+
In the previous example, the alias ``product_details`` was pointing to
1405+
``product_show`` route.
1406+
1407+
To mark the ``product_show`` route as deprecated, you need to "switch" the alias.
1408+
The ``product_show`` become the alias, and will now point to the ``product_details`` route.
1409+
This way, the ``product_show`` alias could be deprecated.
13521410

13531411
.. configuration-block::
13541412

13551413
.. code-block:: yaml
13561414
1357-
new_route_name:
1358-
alias: original_route_name
1415+
# Move the concrete route definition under ``product_details``
1416+
product_details:
1417+
path: /product/{id}
1418+
controller: App\Controller\ProductController::show
1419+
1420+
# Define the alias and the deprecation under the ``product_show`` definition
1421+
product_show:
1422+
alias: product_details
13591423
13601424
# this outputs the following generic deprecation message:
1361-
# 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.
1425+
# 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.
13621426
deprecated:
13631427
package: 'acme/package'
13641428
version: '1.2'
13651429
1366-
# you can also define a custom deprecation message (%alias_id% placeholder is available)
1430+
# or
1431+
1432+
# you can define a custom deprecation message (%alias_id% placeholder is available)
13671433
deprecated:
13681434
package: 'acme/package'
13691435
version: '1.2'
1370-
message: 'The "%alias_id%" route alias is deprecated. Do not use it anymore.'
1436+
message: 'The "%alias_id%" route alias is deprecated. Please use "product_details" instead.'
13711437
13721438
.. code-block:: xml
13731439
@@ -1377,35 +1443,46 @@ you decided not to maintain it anymore), you can deprecate its definition:
13771443
xsi:schemaLocation="http://symfony.com/schema/routing
13781444
https://symfony.com/schema/routing/routing-1.0.xsd">
13791445
1380-
<route id="new_route_name" alias="original_route_name">
1446+
<!-- Move the concrete route definition under ``product_details`` -->
1447+
<route id="product_details" path="/product/{id}" controller="App\Controller\ProductController::show"/>
1448+
1449+
<!-- Define the alias and the deprecation under the ``product_show`` definition -->
1450+
<route id="product_show" alias="product_details">
13811451
<!-- this outputs the following generic deprecation message:
1382-
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 "product_show" route alias is deprecated. You should stop using it, as it will be removed in the future. -->
13831453
<deprecated package="acme/package" version="1.2"/>
13841454
1385-
<!-- you can also define a custom deprecation message (%alias_id% placeholder is available) -->
1455+
<!-- or -->
1456+
1457+
<!-- you can define a custom deprecation message (%alias_id% placeholder is available) -->
13861458
<deprecated package="acme/package" version="1.2">
1387-
The "%alias_id%" route alias is deprecated. Do not use it anymore.
1459+
The "%alias_id%" route alias is deprecated. Please use "product_details" instead.
13881460
</deprecated>
13891461
</route>
13901462
</routes>
13911463
13921464
.. code-block:: php
13931465
1394-
$routes->alias('new_route_name', 'original_route_name')
1466+
$routes->add('product_details', '/product/{id}')
1467+
->controller('App\Controller\ProductController::show');
1468+
1469+
$routes->alias('product_show', 'product_details')
13951470
// this outputs the following generic deprecation message:
1396-
// 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.
1471+
// 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.
13971472
->deprecate('acme/package', '1.2', '')
13981473
1399-
// you can also define a custom deprecation message (%alias_id% placeholder is available)
1474+
// or
1475+
1476+
// you can define a custom deprecation message (%alias_id% placeholder is available)
14001477
->deprecate(
14011478
'acme/package',
14021479
'1.2',
1403-
'The "%alias_id%" route alias is deprecated. Do not use it anymore.'
1480+
'The "%alias_id%" route alias is deprecated. Please use "product_details" instead.'
14041481
)
14051482
;
14061483
1407-
In this example, every time the ``new_route_name`` alias is used, a deprecation
1408-
warning is triggered, advising you to stop using that alias.
1484+
In this example, every time the ``product_show`` alias is used, a deprecation
1485+
warning is triggered, advising you to stop using this route and prefer using ``product_details``.
14091486

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.