-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[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
welcoMattic
wants to merge
2
commits into
symfony:7.3
Choose a base branch
from
welcoMattic:alias-route-attribute
base: 7.3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 indicates 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 | ||
|
@@ -1416,6 +1452,15 @@ Instead of duplicating the original route, you can create an alias for it. | |
In this example, both ``product_show`` and ``product_details`` routes can | ||
be used in the application and will produce the same result. | ||
|
||
.. note:: | ||
|
||
Using non-attributes formats (YAML, XML and PHP) is the only way | ||
to define an alias pointing to a route that you don't own. | ||
|
||
So that you can use your own route name for URL generation, | ||
while actually using a route defined by a third-party bundle as the target of that URL generation, | ||
as the 2 definitions are not required to be in the same config file (or even in the same format). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @OskarStark @stof I've added this side note, is it ok for you? |
||
|
||
.. _routing-alias-deprecation: | ||
|
||
Deprecating Route Aliases | ||
|
@@ -1436,6 +1481,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`` | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.