Map query params to controller arguments with Attributes #52185
Replies: 4 comments 5 replies
-
Hi. Can you please explain the gains of doing this? Why
is not enough? Why add overhead to the framework? |
Beta Was this translation helpful? Give feedback.
-
It's certainly not a necessary feature. It's just a convenient alternative. And it's just a simple proof of concept. If you look at the Symfony link you can see that there are lots of other possibilities. Like mapping the query parameters to a DTO. Regarding overhead, I'm pretty sure it's negligible. |
Beta Was this translation helpful? Give feedback.
-
Jealous thieves |
Beta Was this translation helpful? Give feedback.
-
Can you achieve this via custom contextual attributes container binding? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I just noticed that Symfony has the ability to inject query params as controller arguments by using attributes: https://symfony.com/doc/current/controller.html#mapping-query-parameters-individually
Having no experience whatsoever with how dependency injection works in Laravel, I obviously had to give it a try 😆.
Here's the proof of concept I came up with. I'm not claiming that this is the way to do it, I just wanted to hear if you guys have any thoughts. Wouldn't this be a nice feature to have in Laravel? I haven't looked into it, but I suppose it might be possible to reuse some of the logic from Symfony.
In Illuminate\Routing\ResolvesRouteDependencies
And then inject query params in controller actions like this:
The argument type is optional, and you can give it a default value as usual.
EDIT: I've made a quick prototype package that adds this feature. It uses Symfony's
QueryParameterValueResolver
to resolve, validate and cast the values. https://github.com/mortenscheel/inject-query-paramsBeta Was this translation helpful? Give feedback.
All reactions