Skip to content

[Routing] Add Attribute code examples for alias in #[Route] attribute #20638

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 7.3
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,23 @@ have been renamed. Let's say you have a route called ``product_show``:

.. configuration-block::

.. code-block:: php-attributes

// src/Controller/ProductController.php
namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class ProductController
{
#[Route('/product/{id}', name: 'product_show')]
public function show(): Response
{
// ...
}
}

.. code-block:: yaml

# config/routes.yaml
Expand Down Expand Up @@ -1376,6 +1393,25 @@ Instead of duplicating the original route, you can create an alias for it.

.. configuration-block::

.. code-block:: php-attributes

// src/Controller/ProductController.php
namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class ProductController
{
// "alias" named argument refers to the name of the alias you want to create.
// The alias will point to the actual route "product_show"
#[Route('/product/{id}', name: 'product_show', alias: ['product_details'])]
public function show(): Response
{
// ...
}
}

.. code-block:: yaml

# config/routes.yaml
Expand Down Expand Up @@ -1436,6 +1472,42 @@ This way, the ``product_show`` alias could be deprecated.

.. configuration-block::

.. code-block:: php-attributes

// src/Controller/ProductController.php
namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class ProductController
{
// this outputs the following generic deprecation message:
// 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.
#[Route('/product/{id}',
name: 'product_details',
alias: new DeprecatedAlias(
aliasName: 'product_show',
package: 'acme/package',
version: '1.2',
),
)]
// Or, you can also define a custom deprecation message (%alias_id% placeholder is available)
#[Route('/product/{id}',
name: 'product_details',
alias: new DeprecatedAlias(
aliasName: 'product_show',
package: 'acme/package',
version: '1.2',
message: 'The "%alias_id%" route alias is deprecated. Please use "product_details" instead.',
),
)]
public function show(): Response
{
// ...
}
}

.. code-block:: yaml

# Move the concrete route definition under ``product_details``
Expand Down