Skip to content
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

[Routing] Add {foo:bar} syntax to define a mapping between a route parameter and its corresponding request attribute #54720

Merged
merged 1 commit into from May 2, 2024

Conversation

nicolas-grekas
Copy link
Member

@nicolas-grekas nicolas-grekas commented Apr 24, 2024

Q A
Branch? 7.1
Bug fix? no
New feature? yes
Deprecations? no
Issues -
License MIT

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:

#[Route('/conference/{slug:conference}')]

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:

#[Route('/conference/{id:conference}/{slug:conference}')]

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.

@carsonbot carsonbot added this to the 7.1 milestone Apr 24, 2024
@nicolas-grekas nicolas-grekas modified the milestones: 7.1, 7.2 Apr 24, 2024
@nicolas-grekas nicolas-grekas changed the title [Routing] Add {foo:bar} syntax to define a mapping between a route parameter and its corresponding attribute [Routing] Add {foo:bar} syntax to define a mapping between a route parameter and its corresponding request attribute Apr 24, 2024
@nicolas-grekas nicolas-grekas modified the milestones: 7.2, 7.1 May 2, 2024
…parameter and its corresponding request attribute
@fabpot
Copy link
Member

fabpot commented May 2, 2024

Thank you @nicolas-grekas.

@fabpot fabpot merged commit 15956b2 into symfony:7.1 May 2, 2024
4 of 10 checks passed
fabpot added a commit that referenced this pull request May 2, 2024
… favor of mapped route parameters (nicolas-grekas)

This PR was merged into the 7.1 branch.

Discussion
----------

[DoctrineBridge] Deprecate auto-mapping of entities in favor of mapped route parameters

| Q             | A
| ------------- | ---
| Branch?       | 7.1
| Bug fix?      | no
| New feature?  | yes
| Deprecations? |
| Issues        | Fix #50739
| License       | MIT

Auto-mapping of entities on controllers is a foot-gun when multiple entities are listed on the signature of the controller.
This is described extensively by e.g. `@stof` in the linked issue and in a few others.

The issue is that we try to use all request attributes to call a `findOneBy`, but when there are many entities, there can be an overlap in the naming of the field/associations of both entities.

In this PR, I propose to deprecate auto-mapping and to replace it with mapped route parameters, as introduced in #54720.

If we go this way, people that use auto-mapping to e.g. load a `$conference` based on its `{slug}` will have to declare the mapping by using `{slug:conference}` instead. That makes everything explicit and keeps a nice DX IMHO, by not forcing a `#[MapEntity]` attribute for simple cases.

Commits
-------

a49f9ea [DoctrineBridge] Deprecate auto-mapping of entities in favor of mapped route parameters
@fabpot fabpot mentioned this pull request May 2, 2024
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.

None yet

3 participants