[Routing] Add {foo:bar}
syntax to define a mapping between a route parameter and its corresponding request attribute
#54720
+128
−5
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.
While trying to improve the DX of auto-mapping Doctrine entities and from the discussion on the related PR #54455, I realized that it would help a lot if we were able to express a mapping between route parameter and request attributes directly into route definitions.
This PR adds the ability to define a route with such a mapping:
On the router side, the side-effect of this is just that a new
_route_mapping
array is returned by the matcher, and nothing else.Then, in HttpKernel's RouterListener, we use that parameter to map route parameters to request attributes. On their turn, argument resolvers will just see a request attribute named
conference
. But they can also now read the_route_mapping
attribute and decide to do more tailored things depending on the mapping.For example, one could define this route:
This would be turned into a request attribute named
conference
with['id' => 'the-id', 'slug' => 'the-slug']
as content.This mapping concern already leaks into many value resolver attributes (see their "name" property).
For the entity value resolver, this feature will allow deprecating auto-mapping altogether, and will make things more explicit.