Reputation: 15047
I have a controller and i'm using 3 middlewares inside my controller.
public function __construct(){
$this->middleware('auth');
$this->middleware('isAgency');
$this->middleware('isAdmin');
}
The problem is that Laravel applies all of them at once....if i'm logged in as admin i can't open the index and if i'm logged in as agency again i can't open the index. What i want is that user can be Admin or Agency to access all the functions inside this controller.
So is there some kind of a way to apply one or another middleware?
Like if(isAdmin || isAgency)
?
Upvotes: 2
Views: 3040
Reputation: 467
there is to options to do it
option 1: the simplest way is to create a new middleware that contains the three of them. for example
public function handle($request, Closure $next)
{
// either one of this meet
if (Auth::user()->isAgency() || Auth::user()->isAdmin) {
return $next($request);
}
// return the response
}
option 2: is to use the parameters for the middleware it work only for laravel 5.* laravel middleware parameters
Upvotes: 3
Reputation: 15047
I have solved my problem using 2 middlewares in one controller with this solution:
In my middleware file before:
public function handle($request, Closure $next)
{
if (Auth::user()->isAgency()) {
return $next($request);
}
abort(403);
}
and now i have added isAdmin() middleware so the admin can access everything:
public function handle($request, Closure $next)
{
if (Auth::user()->isAgency() || Auth::user()->isAdmin()) {
return $next($request);
}
abort(403);
}
and inside controller i just use middleware isAgency and admin is also included
public function __construct(){
$this->middleware('auth');
$this->middleware('isAgency');
}
Upvotes: 0
Reputation: 149
Well the simple is that you can declare three different controllers for three middlewares
This can be used for auth
controller
public function __construct(){
$this->middleware('auth');
}
This can be used for isAdmin
controller
public function __construct(){
$this->middleware('isAdmin');
}
This can be used for isAgency
controller
public function __construct(){
$this->middleware('isAgency');
}
Upvotes: 0