Edelco

Laravel multiple parameters in Route middelware not working

Posted 1 year ago by Edelco

I have problem that using multiple parameters in my Route::middleware isn't working for me. I am trying to assign a specific route only accessible for a superuser and admin role.

When I just use:

role:superuser it works fine, but when I add a second parameter like:

role:superuser,admin it fails when I assign myself the admin role but still works for the superuser role.

I am confused so any help would be appreciated!

Here is my RoleMiddleware:

namespace App\Http\Middleware;

use Closure;

class RoleMiddleware {

/**

  • Handle an incoming request.
  • @param \Illuminate\Http\Request $request
  • @param \Closure $next
  • @param string $roles
  • @return mixed */

public function handle($request, Closure $next, ...$roles) { $user = $request->user();

if ($user && $user->isSuperuser($roles)) {
    return $next($request);
}

return redirect('/home')->withError('U heeft niet de juiste rechten!');

}

}

Here is my isSuperuser method in my User model:

public function isSuperuser(...$roles) { if ($roles) { return $this->roles == $roles; }

return $this->roles;

}

Last but not least my routes/web code for the middleware:

Route::get('/users', '[email protected]')->middleware(['role:superuser,admin']); Btw: the method is called 'isSuperuser' but that's just a name. It also has to accept the admin role at some point.

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.