-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
Laravel contextual binding does not work with form request #41195
Comments
Contextual binding works for classes' constructors or resolved from the container using Controller actions are not resolved from the container. One option is to resolve it on the controller itself: use App\Http\Requests\BarRequest;
use Illuminate\Http\Request;
public function index(Request $request)
{
$request = tap(BarRequest::createFrom($request), fn($request) => $request->validateResolved());
dd($request::class);
} Another is to seek help in the support channels: https://laravel.com/docs/9.x/contributions#support-questions |
@rodrigopedra The request and other services that are typehinted on a controller action method ARE resolved from the container using https://github.com/laravel/framework/blob/v9.2.0/src/Illuminate/Routing/ControllerDispatcher.php#L40 I don't think contextual binding works for non-constructor dependencies. In order to accomplish what the issue author wants, method binding should be used. Unfortunately it's not really documented on the container documentation page. The only mention of it is on the queue page (but at least it shows how it's done): https://laravel.com/docs/9.x/queues#handle-method-dependency-injection |
Thanks for the heads up @X-Coder264 , I know that as you can read on this phrase:
Maybe what is confusing on my text is the usage of "or" instead of "and". It was late down here and I had phrased it first in a different way and I might have overseen how the phrase ended. I meant that it wouldn't work for non-constructors methods. But your comment makes it way clearer, thanks :) |
One more thing the code snippet I was referring to when I wrote
framework/src/Illuminate/Routing/ControllerDispatcher.php Lines 38 to 49 in 6c0d272
Which as you can see shows that controller actions are not resolved from the container. Maybe I could have phrased it better. |
Yeah, the entire sentence |
@X-Coder264 this is what I wanted to mean. Thanks! Sorry English is not my native language. Have a nice day =) |
We'd welcome a PR to make that more clearer in the docs. Thanks for helping out @X-Coder264 @rodrigopedra 👍 |
@X-Coder264 could you help me with an example based on my code? I have some trouble implementing it |
@driesvints can you reopen this? I tried this code and didn't work.
This function will never be called. Please provide me with a working example or at least look into what I am doing wrong. |
@Temepest74 you can read the motivation to add this feature on its original PR: The idea behind And also it only works for methods that are called from the container, using Controller actions are NOT called from the container. So you won't be able to decorate them using this approach. Maybe you could extend your controller, and tell the container to resolve your extended class that overrides that method/action based on condition. Please, further questions should be directed to the support channels, as according to the docs:
reference: https://laravel.com/docs/9.x/contributions#support-questions See the link above for the support channels links. |
So, you just said this is impossible to do? After your colleague approved what the other people said (they said it is possible) |
First I wrote:
Here it refers to contextual bindings for methods. Then I wrote:
Which is true, and no other answer says the opposite. What @X-Coder264 wrote is:
Which means the dependencies are resolved from the container, not the method calling. If you look at the code linked in that answer you will see the And in the PR I linked above it says contextual bindings for methods are only applied to methods called from the container. Which a controller's method is not. Resolving dependencies from the container is different than calling a method from the container. Please send further questions to the support channels I linked above per Laravel's policy. |
Oh, ok my bad. I was unable to understand these terms. |
No worries, English is not my native language, I might have worded it badly :) |
Laravel contextual binding does not work with requests, my example will demonstrate what do I mean.
Now, If I use the below code inside RouteServiceProvider, the ControllerB won't have the RequestA bound to it.
The text was updated successfully, but these errors were encountered: