Answer the question
In order to leave comments, you need to log in
Is it possible to use middleware in a controller?
There is a ShopController controller, it has a standard 'show' method.
So, when /api/shops/some_id is passed, then ->middleware(['auth:api']) does not need to be used for this.
But when there is a parameter, for example ?fields =balance, then you need to check the user type.
That is, it turns out that I don’t need this stub for the entire method, but only with some parameters.
But I can’t use middleware in the method itself.
Here I do this:
if (in_array('balance', $params)){
$this->middleware(['auth:api']);
return $this->setData(['test' => 1])->renderOutput();
}
Answer the question
In order to leave comments, you need to log in
middleware doesn't work that way. They either execute their logic first and then tell the next one in the chain to "run", or they first tell the next one to "run" and then respond. Actually, this chain is a set of middlewares in the order in which they are specified, and somewhere there, if this business is started, there will be a call to the controller method.
In general, it's more like authorization than authentication, so you can use or something like that. Well, or check authentication, as you wanted:
Better without facades, of course. $this->authorize('shops.filter')
Auth::authenticate()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question