Skip to content

[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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2025

Conversation

eltharin
Copy link
Contributor

@eltharin eltharin commented Mar 3, 2025

Q A
Branch? 7.3
Bug fix? no
New feature? yes
Deprecations? no
License MIT

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 :

    public function 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'}) }}

@carsonbot carsonbot added this to the 7.3 milestone Mar 3, 2025
@OskarStark OskarStark changed the title [Routing] Add alias in {foo:bar} syntax in route parameter [Routing] Add alias in {foo:bar} syntax in route parameter Mar 4, 2025
@eltharin
Copy link
Contributor Author

eltharin commented Mar 4, 2025

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 commented Mar 4, 2025

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
Contributor Author

eltharin commented Mar 4, 2025

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
Contributor Author

eltharin commented Mar 4, 2025

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
Contributor Author

eltharin commented Mar 4, 2025

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

@eltharin
Copy link
Contributor Author

eltharin commented Mar 4, 2025

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

@eltharin eltharin requested a review from OskarStark March 7, 2025 07:34
@nicolas-grekas
Copy link
Member

nicolas-grekas commented Mar 17, 2025

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
Contributor Author

Do you prefer this new description ?

@fabpot fabpot force-pushed the aliasroutemapping branch from 3cfef01 to 4e2c638 Compare March 24, 2025 08:06
@fabpot
Copy link
Member

fabpot commented Mar 24, 2025

Thank you @eltharin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants