[Routing] Add RoutableInterface allowing objects to provide routing parameters #61038
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.
🚀 New Feature: Simplify Router Parameter Generation with Routable Objects
This pull request introduces the ability to pass objects directly as parameters to the Symfony Router, significantly simplifying the manual provision of complex parameter structures for routes. This improves code readability and reduces the chance of errors when generating URLs, especially with deeply nested or lengthy URL patterns.
💡 The Problem This Solves
In many Symfony applications, URL generation is a common task. For complex route patterns, such as
/season/{season}/competition/{competition}/pools/{pool}/matches/{match}
, developers currently have to manually collect all necessary parameters and pass them to the router. For example:This quickly becomes cumbersome, error-prone, and reduces code readability, especially when routes require many parameters or when the objects containing the data are deeply nested.
✨ The Proposed Solution
This PR introduces a new concept where objects can define their own "router parameters" by implementing a specific interface. This allows the router to automatically extract the necessary parameters from the object, rather than requiring them to be manually specified.
Core Concepts:
RoutableInterface
: A new interface that objects can implement. This interface defines a method (e.g.,getRouterParameters()
) that returns aRouterParameters
object.RouterParameters
Value Object: A new value object that can hold a structured collection of parameters, including the ability to nest otherRoutableInterface
implementations.Example Usage:
Let's say we have
Pool
andMatch
classes that implement theRoutableInterface
:With this implementation, URLs can be generated much more intuitively:
🌟 Benefits
getRouterParameters()
method in the respective objects, instead of modifying allgenerate()
calls.🙏 Feedback Welcome!
This is my first feature pull request for Symfony, and I'm very eager to receive feedback. Any suggestions to improve the implementation or the proposal are greatly appreciated! I hope this feature can be valuable to the Symfony community.