Skip to content

Commit 22cd58e

Browse files
welcoMatticjaviereguiluz
authored andcommitted
[Routing] Add Attribute code examples for alias in #[Route] attribute
1 parent 4b5d780 commit 22cd58e

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

routing.rst

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,23 @@ have been renamed. Let's say you have a route called ``product_show``:
13401340

13411341
.. configuration-block::
13421342

1343+
.. code-block:: php-attributes
1344+
1345+
// src/Controller/ProductController.php
1346+
namespace App\Controller;
1347+
1348+
use Symfony\Component\HttpFoundation\Response;
1349+
use Symfony\Component\Routing\Attribute\Route;
1350+
1351+
class ProductController
1352+
{
1353+
#[Route('/product/{id}', name: 'product_show')]
1354+
public function show(): Response
1355+
{
1356+
// ...
1357+
}
1358+
}
1359+
13431360
.. code-block:: yaml
13441361
13451362
# config/routes.yaml
@@ -1376,6 +1393,25 @@ Instead of duplicating the original route, you can create an alias for it.
13761393

13771394
.. configuration-block::
13781395

1396+
.. code-block:: php-attributes
1397+
1398+
// src/Controller/ProductController.php
1399+
namespace App\Controller;
1400+
1401+
use Symfony\Component\HttpFoundation\Response;
1402+
use Symfony\Component\Routing\Attribute\Route;
1403+
1404+
class ProductController
1405+
{
1406+
// "alias" named argument indicates the name of the alias you want to create.
1407+
// The alias will point to the actual route "product_show"
1408+
#[Route('/product/{id}', name: 'product_show', alias: ['product_details'])]
1409+
public function show(): Response
1410+
{
1411+
// ...
1412+
}
1413+
}
1414+
13791415
.. code-block:: yaml
13801416
13811417
# config/routes.yaml
@@ -1416,6 +1452,15 @@ Instead of duplicating the original route, you can create an alias for it.
14161452
In this example, both ``product_show`` and ``product_details`` routes can
14171453
be used in the application and will produce the same result.
14181454

1455+
.. note::
1456+
1457+
Using non-attributes formats (YAML, XML and PHP) is the only way
1458+
to define an alias pointing to a route that you don't own.
1459+
1460+
So that you can use your own route name for URL generation,
1461+
while actually using a route defined by a third-party bundle as the target of that URL generation,
1462+
as the 2 definitions are not required to be in the same config file (or even in the same format).
1463+
14191464
.. _routing-alias-deprecation:
14201465

14211466
Deprecating Route Aliases
@@ -1436,6 +1481,42 @@ This way, the ``product_show`` alias could be deprecated.
14361481

14371482
.. configuration-block::
14381483

1484+
.. code-block:: php-attributes
1485+
1486+
// src/Controller/ProductController.php
1487+
namespace App\Controller;
1488+
1489+
use Symfony\Component\HttpFoundation\Response;
1490+
use Symfony\Component\Routing\Attribute\Route;
1491+
1492+
class ProductController
1493+
{
1494+
// this outputs the following generic deprecation message:
1495+
// 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.
1496+
#[Route('/product/{id}',
1497+
name: 'product_details',
1498+
alias: new DeprecatedAlias(
1499+
aliasName: 'product_show',
1500+
package: 'acme/package',
1501+
version: '1.2',
1502+
),
1503+
)]
1504+
// Or, you can also define a custom deprecation message (%alias_id% placeholder is available)
1505+
#[Route('/product/{id}',
1506+
name: 'product_details',
1507+
alias: new DeprecatedAlias(
1508+
aliasName: 'product_show',
1509+
package: 'acme/package',
1510+
version: '1.2',
1511+
message: 'The "%alias_id%" route alias is deprecated. Please use "product_details" instead.',
1512+
),
1513+
)]
1514+
public function show(): Response
1515+
{
1516+
// ...
1517+
}
1518+
}
1519+
14391520
.. code-block:: yaml
14401521
14411522
# Move the concrete route definition under ``product_details``

0 commit comments

Comments
 (0)