Skip to content

Commit

Permalink
docs(routing): Add Attribute code examples for alias in #[Route] attr…
Browse files Browse the repository at this point in the history
…ibute
  • Loading branch information
welcoMattic committed Feb 9, 2025
1 parent 0b180d5 commit 5ccc81e
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1307,8 +1307,32 @@ Route Aliasing

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

.. note::

In next examples, ``original_route_name`` was the default name of the route,
now it's ``new_route_name``, while ``original_route_name`` is still available
as an alias.

.. configuration-block::

.. code-block:: php-attributes
// src/Controller/DefaultController.php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class DefaultController extends AbstractController
{
#[Route('/path', name: 'new_route_name', alias: ['alias', 'original_route_name'])]
public function action(): Response
{
// ...
}
}
.. code-block:: yaml
# config/routes.yaml
Expand Down Expand Up @@ -1349,6 +1373,44 @@ you decided not to maintain it anymore), you can deprecate its definition:

.. configuration-block::

.. code-block:: php-attributes
// src/Controller/DefaultController.php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class DefaultController extends AbstractController
{
// this outputs the following generic deprecation message:
// 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.
#[Route('/path',
name: 'new_route_name',
alias: new DeprecatedAlias('original_route_name', 'MyBundleFixture', '1.0')
)]
// you can also define a custom deprecation message (%alias_id% placeholder is available)
#[Route('/path',
name: 'new_route_name',
alias: new DeprecatedAlias('original_route_name', 'acme/package', '1.2', message: 'The "%alias_id%" route alias is deprecated. Do not use it anymore.')
)]
public function action(): Response
{
// ...
}
#[Route('/path', name: 'new_route_name', alias: [
new DeprecatedAlias('first_original_route_name', 'acme/package', '1.0'),
new DeprecatedAlias('second_original_route_name', 'acme/package', '2.0'),
new DeprecatedAlias('third_original_route_name', 'acme/package', '3.0'),
])]
public function another_action()
{
// ...
}
}
.. code-block:: yaml
new_route_name:
Expand Down

0 comments on commit 5ccc81e

Please sign in to comment.