Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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

Merged
fabpot merged 1 commit intosymfony:7.3fromdamienfern:feat/route-alias
Jan 25, 2025

Conversation

damienfern
Copy link
Contributor

@damienferndamienfern commentedNov 9, 2024
edited
Loading

QA
Branch?7.3
Bug fix?no
New feature?yes
Deprecations?no
LicenseMIT

While scrolling theRouting documentation, I noticed that we can't configure aliases with the#[Route] attribute.

With this PR, we can.

#[Route('/path', name:'action_with_alias', alias: ['alias','completely_different_name'])]publicfunctionaction(){}

welcoMattic and hason reacted with thumbs up emoji
@carsonbotcarsonbot added this to the7.2 milestoneNov 9, 2024
@carsonbotcarsonbot changed the title[WIP] [Routing] set aliases in Route attribute[Routing] [WIP] set aliases in Route attributeNov 9, 2024
@damienferndamienfern changed the title[Routing] [WIP] set aliases in Route attribute[WIP] [Routing] Add aliases in Route attributeNov 9, 2024
@carsonbotcarsonbot changed the title[WIP] [Routing] Add aliases in Route attribute[Routing] [WIP] Add aliases in Route attributeNov 9, 2024
@smnandre
Copy link
Member

Genuine curiosity questions here:

  • How would this work with aliases on the controller class attribute and others on the method attribute ?
  • Same question if aliases are defined on the groupe (in yaml for instance) and then on the controller ?

Are they all combined ?

@OskarStarkOskarStark changed the title[Routing] [WIP] Add aliases in Route attribute[Routing] [WIP] Add aliases in Route attributeNov 10, 2024
@damienferndamienfernforce-pushed thefeat/route-alias branch 2 times, most recently fromb81292e to093fce4CompareNovember 12, 2024 08:07
@OskarStarkOskarStark changed the title[Routing] [WIP] Add aliases in Route attribute[Routing] [WIP] Allow aliases in#[Route] attributeNov 12, 2024
@damienfern
Copy link
ContributorAuthor

Genuine curiosity questions here:

* How would this work with aliases on the controller class attribute and others on the method attribute ?* Same question if aliases are defined on the groupe (in yaml for instance) and then on the controller ?

Are they all combined ?

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 theAttributeClassLoader::addRoute) and it needs either the __invoke method or the method where the attribute is used.

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$this->redirectToRoute('my_alias') ? It might be good to trigger an error if devs tries to set aliases on a class instead of on a method. What do you think ?

@smnandre
Copy link
Member

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.

So no "combined aliases" (invoyable alias + name of the action route), ok!

But how the router will know which route should be called when we use features like$this->redirectToRoute('my_alias') ? It might be good to trigger an error if devs tries to set aliases on a class instead of on a method. What do you think ?

I guess it would be a better DX than not indeed :)

damienfern reacted with thumbs up emoji

@damienfern
Copy link
ContributorAuthor

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.

So no "combined aliases" (invoyable alias + name of the action route), ok!

But how the router will know which route should be called when we use features like$this->redirectToRoute('my_alias') ? It might be good to trigger an error if devs tries to set aliases on a class instead of on a method. What do you think ?

I guess it would be a better DX than not indeed :)

Done :)

smnandre reacted with thumbs up emoji

@damienferndamienfern changed the title[Routing] [WIP] Allow aliases in#[Route] attribute[Routing] Allow aliases in#[Route] attributeNov 19, 2024
@damienfern
Copy link
ContributorAuthor

Status : Needs Review

@fabpotfabpot modified the milestones:7.2,7.3Nov 20, 2024
Copy link
Member

@GromNaNGromNaN left a comment
edited
Loading

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 = [],
Copy link
Member

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.

damienfern reacted with thumbs up emoji
@stof
Copy link
Member

This proposal does not allow configuring the alias as deprecated (once it is a common use case for aliases)

{
if (\is_array($aliases)) {
foreach ($aliasesas$a) {
if (!\is_string($a)) {
Copy link
Member

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
Copy link
Member

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
Copy link
Member

GromNaN commentedJan 13, 2025
edited
Loading

This proposal does not allow configuring the alias as deprecated (once it is a common use case for aliases)

The attribute could accept an instance ofSymfony\Component\Routing\Alias, or maybe a subclassDeprecatedAlias with the deprecation properties set in the constructor.

@damienfern
Copy link
ContributorAuthor

This proposal does not allow configuring the alias as deprecated (once it is a common use case for aliases)

The attribute could accept an instance ofSymfony\Component\Routing\Alias, or maybe a subclassDeprecatedAlias with the deprecation properties set in the constructor.

IsSymfony\Component\Routing\Aliasreally needed ? According to thedoc, aliases can be set by string or deprecated record. So this is what I suggest :

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 ?

@GromNaN
Copy link
Member

I realize my solution suggestion was incorrect as theAlias class hold the name of the route to alias, not the name of the alias itself.

Incorrect solution

TheclassAlias already exists and is used by theRouteCollection.

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 theSymfony\Component\Routing\Attribute namespace (even if it's not an attribute class).

@stof
Copy link
Member

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)

GromNaN and mtarld reacted with thumbs up emoji

@damienfern
Copy link
ContributorAuthor

Status: Needs Review

@fabpot
Copy link
Member

Thank you@damienfern.

@fabpotfabpot merged commitd8b3cc6 intosymfony:7.3Jan 25, 2025
3 of 10 checks passed
@fabpotfabpot mentioned this pull requestMay 2, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@rosierrosierrosier left review comments

@stofstofstof left review comments

@OskarStarkOskarStarkOskarStark left review comments

@94noni94noni94noni left review comments

@smnandresmnandresmnandre left review comments

@alexandre-dauboisalexandre-dauboisalexandre-daubois left review comments

@fabpotfabpotfabpot approved these changes

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

@GromNaNGromNaNGromNaN approved these changes

@mtarldmtarldmtarld approved these changes

Assignees
No one assigned
Projects
None yet
Milestone
7.3
Development

Successfully merging this pull request may close these issues.

12 participants
@damienfern@smnandre@fabpot@GromNaN@nicolas-grekas@stof@rosier@OskarStark@94noni@alexandre-daubois@mtarld@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp