A
A
anka20022020-08-05 11:11:33
Laravel
anka2002, 2020-08-05 11:11:33

How to force the vendor middleware to be executed before the execution of the native Request class?

So, I create the Request class with the make:request artisan command in order to validate the request data inside it. From the documentation it is clear that inside the request class created in this way there is an authorize method, by redefining the body of which, you can make preliminary boolean checks related to authorization.

class SuggestionRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return $this->user() && $this->user()->hasRole('sales manager');
//        return true;
    }
}

So I decided to do it initially so that the check for an authorized user and his role is performed before the Request data validation checks, BUT a third-party middleware auth: sanctum is attached to the route
Route::post('/suggest', [SuggestionController::class, 'store'])
    ->middleware('auth:sanctum');

and, it turns out that now they together do similar work, but at different points in time. So, the tests make it clear that the sequence of checks is built as follows:
1) in the authorize method (Request class)
2) data validation (within the same class)
3) the same checks as in the 1st paragraph, only at the level of responsibility third party middleware (in my case auth:sanctum).
==
I would like the validation inside the third-party middleware to be done BEFORE the Request class is validated (2nd point).
Please, I ask you to suggest possible options for how one could act in my case), in particular, how one should refactor, if necessary.
==
I also adopted beforeMiddleware from the docks, but it makes sense only with custom intermediaries ..?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shamanov, 2020-08-05
@SilenceOfWinter

https://laravel.com/docs/7.x/routing#route-group-m...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question