laravel-actions
laravel-actions copied to clipboard
authorize() method dependencies are not resolved as expected
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