Skip to content

Commit 848f249

Browse files
committed
docs(routing): Add Attribute code examples for alias in #[Route] attribute
1 parent 4b5d780 commit 848f249

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

routing.rst

Lines changed: 72 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 refers to 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
@@ -1436,6 +1472,42 @@ This way, the ``product_show`` alias could be deprecated.
14361472

14371473
.. configuration-block::
14381474

1475+
.. code-block:: php-attributes
1476+
1477+
// src/Controller/ProductController.php
1478+
namespace App\Controller;
1479+
1480+
use Symfony\Component\HttpFoundation\Response;
1481+
use Symfony\Component\Routing\Attribute\Route;
1482+
1483+
class ProductController
1484+
{
1485+
// this outputs the following generic deprecation message:
1486+
// 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.
1487+
#[Route('/product/{id}',
1488+
name: 'product_details',
1489+
alias: new DeprecatedAlias(
1490+
aliasName: 'product_show',
1491+
package: 'acme/package',
1492+
version: '1.2',
1493+
),
1494+
)]
1495+
// Or, you can also define a custom deprecation message (%alias_id% placeholder is available)
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+
message: 'The "%alias_id%" route alias is deprecated. Please use "product_details" instead.',
1503+
),
1504+
)]
1505+
public function show(): Response
1506+
{
1507+
// ...
1508+
}
1509+
}
1510+
14391511
.. code-block:: yaml
14401512
14411513
# Move the concrete route definition under ``product_details``

0 commit comments

Comments
 (0)