Skip to content

Commit 0b3db8e

Browse files
committed
minor #20638 [Routing] Add Attribute code examples for alias in #[Route] attribute (welcoMattic)
This PR was squashed before being merged into the 7.3 branch. Discussion ---------- [Routing] Add Attribute code examples for alias in `#[Route]` attribute Fixes #20600 This one adds code examples to alias route in `#[Route]` attribute. It also add example using `DeprecatedAlias`. I've also added a note to precise `new_route_name` and `original_route_name` meaning, to avoid misunderstanding. cc `@damienfern` Commits ------- 22cd58e [Routing] Add Attribute code examples for alias in `#[Route]` attribute
2 parents 1386a8f + 22cd58e commit 0b3db8e

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
@@ -1342,6 +1342,23 @@ have been renamed. Let's say you have a route called ``product_show``:
13421342

13431343
.. configuration-block::
13441344

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

13791396
.. configuration-block::
13801397

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

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

14231468
Deprecating Route Aliases
@@ -1438,6 +1483,42 @@ This way, the ``product_show`` alias could be deprecated.
14381483

14391484
.. configuration-block::
14401485

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

0 commit comments

Comments
 (0)