Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.6k
[Routing] Allow aliases in#[Route]
attribute#58819
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Genuine curiosity questions here:
Are they all combined ? |
b81292e
to093fce4
Compare#[Route]
attributeUh oh!
There was an error while loading.Please reload this page.
For now, setting aliases on non invokable controller class does nothing with it. The reason is that aliases from the attribute are added when creating the route (handled by the IMO, aliases should not be used on class (unless it is invokable). Setting an alias adds another name to a route and if we use alias on a class, it means that all routes declared in this class will have the same alias. But how the router will know which route should be called when we use features like |
So no "combined aliases" (invoyable alias + name of the action route), ok!
I guess it would be a better DX than not indeed :) |
cdb3dd7
to7c76ef2
Compare
Done :) |
7c76ef2
toabf4cf2
CompareUh oh!
There was an error while loading.Please reload this page.
6e81be1
to749c442
Compare#[Route]
attribute#[Route]
attributeStatus : Needs Review |
src/Symfony/Component/Routing/Tests/Fixtures/AttributeFixtures/AliasClassController.phpShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
GromNaN left a comment• edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Until now, you couldn't add an alias to a route configuration. Aliases are defined independently of the route. This is an inversion of the current configuration logic.
Addingaliases
parameter to the#[Route]
attribute is equivalent to adding it to the Yaml configuration:
- blog_index:- alias: blog_list blog_list: path: /blog # the controller value has the format 'controller_class::method_name' controller: App\Controller\BlogController::list+ aliases: [blog_index]
Features likedeprecating a route alias are not supported with this configuration way.
@@ -56,6 +57,7 @@ public function __construct( | |||
?bool $utf8 = null, | |||
?bool $stateless = null, | |||
private ?string $env = null, | |||
private array $aliases = [], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Since there will generally be only one alias, we can acceptstring|string[]
for the attribute and name it alias. Convert toarray
in the constructor.
publicfunction __construct(// ...array|string$alias = [], ) {// ...$this->aliases = (array)$alias;
All the test examples usealias
attribute name, which cause the error"Unknown named parameter
$alias`" in tests. It needs to be renamed here.
cae7ff1
toc750a97
CompareThis proposal does not allow configuring the alias as deprecated (once it is a common use case for aliases) |
Uh oh!
There was an error while loading.Please reload this page.
{ | ||
if (\is_array($aliases)) { | ||
foreach ($aliasesas$a) { | ||
if (!\is_string($a)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I don't think we need to be that defensive here. If we have the appropriatestring[]
type in phpdoc, we always consider it as being part of our contract and we generally don't add associated runtime checks (which require looping over the array). Projects wanting extra guarantees can use static analysis to get those benefits.
} | ||
/** | ||
* @param list<string>|string $aliases |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I suggest usingstring[]
instead oflist<string>
to be less restrictive on the input. This will be consistent with other methods (and with the constructor where you don't ask for a list) and the code consuminggetAliases
does not even read the keys so it is fine with any array of strings, not just with lists.
GromNaN commentedJan 13, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
The attribute could accept an instance of |
Is class DeprecatedAlias {privatestring$deprecationMessage;publicfunction__construct(privatestring$aliasName,privatestring$package,privatestring$version,string$message ='Since %package% %version%: The "%aliasName%" route alias is deprecated. You should stop using it, as it will be removed in the future', ){ }} And in Route Attribute : /*** @param string|string[]|DeprecatedAlias|DeprecatedAlias[] $alias The list of aliases for this route*/ What do you think ? |
I realize my solution suggestion was incorrect as the Incorrect solutionTheclass namespaceSymfony\Component\Routing;useSymfony\Component\Routing\Alias;class DeprecatedAliasextends Alias{publicfunction__construct(string$id,string$package,string$version,string$message) {parent::__construct($id);$this->setDeprecated($package,$version,$message); }} And to use: #[Route('/path', name:'route_name', alias:newDeprecatedAlias('legacy_alias','acme/package','10.9','Deprecation message'))]publicfunctionaction(){} I agree agree with the class you suggest, in the |
The class used in the attribute should not extend the Alias class IMO (just like our Route attribute does not reuse the Route class used in the RouteCollection, allowing us to adapt the attribute API separately from the low-level API of the component) |
cb75972
to33a4c1a
CompareUh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Status: Needs Review |
c47b529
to4e71cfe
CompareThank you@damienfern. |
d8b3cc6
intosymfony:7.3Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
While scrolling theRouting documentation, I noticed that we can't configure aliases with the
#[Route]
attribute.With this PR, we can.