Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpKernel] ArgumentResolver should run after kernel.controller_arguments #33205

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

Closed
gmponos opened this issue Aug 16, 2019 · 5 comments
Closed

Comments

@gmponos
Copy link
Contributor

gmponos commented Aug 16, 2019

Description
I saw that @stof created this #18440 PR about adding the new event for the controller arguments.

This is a very useful feature that I would like to use..

What i'd like to achieve is before reaching to the ArgumentResolver to use this event to do some api calls and load some objects.

Then my ArgumentResolver could check from a container that I have if those objects are loaded and if they are then they should be resolved otherwise the flow of the framework must throw exception.

Unfortunately this event is running after the ArgumentResolver.

This order of this raises some questions to me.

  1. In the documentation it says:

https://symfony.com/doc/current/reference/events.html#kernel-controller-arguments

This event is dispatched just before a controller is called. It's useful to configure the arguments that are going to be passed to the controller. Typically, this is used to map URL routing parameters to their corresponding named arguments; or pass the current request when the Request type-hint is found:

How am I supposed to map the URL parameters to my controller parameters... ?? At the moment when a parameter is not nullable AND no Argument resolver can resolve it then (at least at my application) the ServiceValueResolver throws an exception.

  1. Is the current order (meaning executing the event after the ArgumentResolver) made by design?

What i'd like to achieve is before reaching to the ArgumentResolver to use this event to do some api calls and load some objects.

I could do the same inside an ArgumentResolver when the support function is called but if the API fails then an exception must be thrown.. and in general it feels kinda dirty.

Anyway is it possible to change the order inside the HttpKernel?

@linaori
Copy link
Contributor

linaori commented Aug 16, 2019

Regarding 2: I'm not sure why the ServiceValueResolver is throwing an exception in that case, wouldn't use that on action arguments to be honest. Could it be that the typehint can be autowired to something and that causes the exception? Personally would ditch action injection because stateless and stateful mixed is a pain. I prefer to keep my stateless (services) in the constructor arguments of services.

Regarding 3: You can throw exceptions in a resolver. If you ask me, the resolver is the correct place to fetch those objects.

Anyway is it possible to change the order inside the HttpKernel?

I don't think that would make any sense.

@gmponos
Copy link
Contributor Author

gmponos commented Aug 16, 2019

Regarding 3: You can throw exceptions in a resolver. If you ask me, the resolver is the correct place to fetch those objects.

One other thing is that on the Resolver I do not have also the controller instance.. which is quite important for my logic...

Personally would ditch action injection because stateless and stateful mixed is a pain. I prefer to keep my stateless (services) in the constructor arguments of services

Well yes.. my services are injected through the constructor.. but there are some services that need the request in order to be generated... (It's a quite complex domain).. But I can not reach to ControllerArguments... maybe I should remove the ServiceArgumentResolver with a CompilerPass..

You can throw exceptions in a resolver. If you ask me, the resolver is the correct place to fetch those objects.

Yes if you throw exception then you break the chain.. and there might be another ArgumentResolver that actually supports this.

@linaori
Copy link
Contributor

linaori commented Aug 16, 2019

One other thing is that on the Resolver I do not have also the controller instance.. which is quite important for my logic...

What do you need the controller instance for? You can get the configured controller from the request $request->attributes->get('_controller') from the top of my head.

maybe I should remove the ServiceArgumentResolver with a CompilerPass

If you don't tag your controller with the tag and make it public yourself, you won't have the ServiceArgumentResolver pass through it.

Yes if you throw exception then you break the chain.. and there might be another ArgumentResolver that actually supports this.

Supports could return false, but perhaps this isn't the right place to do it then? If you need a very complex structure for this, you could also do it in your controller instead.

@gmponos gmponos changed the title ArgumentResolver should run after kernel.controller_arguments [HttpKernel] ArgumentResolver should run after kernel.controller_arguments Aug 16, 2019
@carsonbot
Copy link

Thank you for this issue.
There has not been a lot of activity here for a while. Has this been resolved?

@carsonbot
Copy link

Friendly ping? Should this still be open? I will close if I don't hear anything.

@chalasr chalasr closed this as completed Jan 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants