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] Add alias in{foo:bar} syntax in route parameter#59904

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.3fromeltharin:aliasroutemapping
Mar 24, 2025

Conversation

eltharin
Copy link
Contributor

@eltharineltharin commentedMar 3, 2025
edited
Loading

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

Since#54720 we can/have to write route parameters with "destination" as slug:bar,

but if we have two properties with same name example :

/search-book/{name:author}/{name:category}

we get the error message : Route pattern "/search-book/{name}/{name}" cannot reference variable name "name" more than once.

Actually to prevent this error we have to use MapEntity as :

publicfunction bookSearch(        #[MapEntity(mapping: ['authorName' =>'name'])]Author$author,        #[MapEntity(mapping: ['categoryName' =>'name'])]Category$category):Response    {

and we have to remove Mapped Route Parameters :

#[Route('/search-book/{authorName}/{categoryName}')

This PR proposal is to remove MapEntity attributes and keep Mapped Route Parameters by adding an alias on it :

/search-book/{authorName:author.name}/{categoryName:category.name}

With that, EntityValueResolver will search name in author Entity and name in Category Entity.

We can have url with :{{ path('bookSearch', {authorName: 'KING', categoryName: 'Horror'}) }}

valtzu reacted with thumbs up emoji
@carsonbotcarsonbot added this to the7.3 milestoneMar 3, 2025
@OskarStarkOskarStark changed the title[Routing] Add alias in {foo:bar} syntax in route parameter[Routing] Add alias in{foo:bar} syntax in route parameterMar 4, 2025
@eltharin
Copy link
ContributorAuthor

Is it a choice to not use named capture in Route.php ?
Or I can do it for better read ?

@nicolas-grekas
Copy link
Member

nicolas-grekas commentedMar 4, 2025
edited
Loading

I'm sorry I don't get this feature, can you expand on what this provides?
Currently, you can have a route with with both{id:foo} and{slug:foo}.
What else do you need?

@eltharin
Copy link
ContributorAuthor

I need a route with {id:foo} and {id:bar}

@nicolas-grekas
Copy link
Member

Why? From a routing PoV, that's undefined: how would you generate just a route for example?

@eltharin
Copy link
ContributorAuthor

that's my proposal, instead of {id:foo} and {id:bar} witch can be impossible, I can have {fooId:foo.id} and {barId:bar.id}.
my route function can have function myRoute( Foo $foo, Bar $bar) and autogenerate them,
and I can generate a route with ['fooId' => 2, 'barId' => 5]

@welcoMattic
Copy link
Member

IIUC, this use case can me handled by:{fooId:foo} and{barId:bar}
function myRoute(Foo $foo, Bar $bar)
{{ path('my_route', { fooId: 1, barId: 1 }) =>my_route/1/1

You do not need to alias anything, just use the proper name for ids.

@eltharin
Copy link
ContributorAuthor

it's just a shortcut to not write Many Map Entity to map alias with field name

@eltharin
Copy link
ContributorAuthor

IIUC, this use case can me handled by:{fooId:foo} and{barId:bar}function myRoute(Foo $foo, Bar $bar){{ path('my_route', { fooId: 1, barId: 1 }) =>my_route/1/1

You do not need to alias anything, just use the proper name for ids.

If your field is id in both classes you must add a mapentity for each arguments.

I just found it quicker to write, and not have to put an attribute to each argument witch is less readable IMO

@nicolas-grekas
Copy link
Member

nicolas-grekas commentedMar 17, 2025
edited
Loading

The description of the PR should be improved IMHO.
I updated it a bit to use slugs instead of random foo/bar; This should help but it's not enough.
Using the example of "id" in the comments doesn't help either: id is already a special field so you don't need to name it.
It'd suggest providing a before/after example with something more concrete to make it more obvious what this is about.
Before: with MapEntity mapping, After: the new shorter way that's being proposed.

@eltharin
Copy link
ContributorAuthor

Do you prefer this new description ?

nicolas-grekas reacted with heart emoji

@fabpot
Copy link
Member

Thank you@eltharin.

@fabpotfabpot merged commit6f598f9 intosymfony:7.3Mar 24, 2025
3 of 9 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

@fabpotfabpotfabpot approved these changes

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

@OskarStarkOskarStarkAwaiting requested review from OskarStark

Assignees
No one assigned
Projects
None yet
Milestone
7.3
Development

Successfully merging this pull request may close these issues.

6 participants
@eltharin@nicolas-grekas@welcoMattic@fabpot@OskarStark@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp