Implicit and explicit route bindings combination with strict parameter positioning #41774
Unanswered
murphatron
asked this question in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey folks! I ran into a situation where I wanted to make use of the parameter scoping feature of routes to ensure the integrity of values being passed to the server for processing. The relationship is a model has a one-to-many relationship with other models of the same class. The intent is maintain version history of a resource. For illustration purposes, a route that looks something like this:
I have the
document
parameter explicitly bound in my service provider like:And lastly, I (want to) have my controller set up like this:
However, this doesn't work. The
state
parameter receives thedocument
instance instead. This is problematic since I don't actually need to do anything with thedocument
parameter in the controller. I only needstate
. To make this work, I would have to define the method parameter for$document
, even though it will have no function. As I mentioned before, I'm choosing to structure the route this way as an extra step to manage the integrity and intent of the requested resource.That led me to dig a bit deeper into the route binding logic to understand what's happening and It seems to stem from the ImplicitRouteBinding class not making use of the
ReflectionParameter->getPosition()
method when setting up the parameters. This is found in the foreach loop set to the$parameter
variable.When it comes to route bindings, I'm aware of at least four things that need to happen.
For implicit bindings and dependency injection, we know exactly where the parameter belongs because of the use of reflection, however, this is ignored by the framework today for implicit bindings. Instead, it appears that the route definition positioning is favored with the exception of injection which is splicing into the correct position. It's my position that when we're aware of explicit positioning of parameters, they should be used. In lieu of that, named positioning should be attempted, then finally, route parameter positioning should be the final fallback.
With all of that being said, I can add a middleware to serve this purpose by removing the unused parameters from the route after route matching though, I argue that this should be a function of the framework itself.
I wanted to bring it here to see what the community thinks about it. I'm curious to see if there are any strong arguments as to why the reflected position should not be used.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions