lorisleiva / laravel-actions

⚡️ Laravel components that take care of one specific task
https://laravelactions.com
MIT License
2.52k stars 124 forks source link

authorize() method dependencies are not resolved as expected #263

Closed h-sigma closed 7 months ago

h-sigma commented 12 months ago

In my application, I have to manually get the route binding-resolved model from the ActionRequest in the authorize method.

public function authorize(ActionRequest $request): void
{
    Gate::authorize('view', [$request->route('attempt')]);
}

public function asController(Attempt $attempt): AttemptResource
{
    $attempt = $this->handle($attempt);
    return new AttemptResource($attempt);
}

If I try to use automatic dependency injection.

public function authorize(Attempt $attempt): void
{
    Gate::authorize('view', [attempt]);
}

the injected Attempt $attempt variable will be an empty model (new Attempt() equivalent) with no data/fields set.

This is because when a controller method (or asController for the Action class) is dispatched, its parameters are resolved via the DI Container + Route Model Binding. Wouldn't it make the most sense to do the same for the authorize method as well?

Especially considering I can't include the AuthorizesRequests trait on the action or write my own "authorize" method to be called directly because it conflicts with the authorize method naming :D

vpratfr commented 9 months ago

Duplicate of #140

h-sigma commented 7 months ago

Cool beananas.