Answer the question
In order to leave comments, you need to log in
middleware options?
Good night, I don’t understand why what is written on the documentation site does not work for me ... I
use standard Laravel authentication, but in addition to everything, there are routes that should be available only to users with specific roles. Based on the documentation, I wrote the following
created the hasRole method in the User model
public function hasRole()
{
return $this->role;
}
public function handle($request, Closure $next, $role)
{
if (!Auth::user()->hasRole($role))
{
return redirect(abort(403));
}
return $next($request);
}
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'role' => \App\Http\Middleware\role::class,
];
$router->group(['prefix'=>'/dashboard', 'middleware'=>'auth'], function($router)
{
$router->group(['middleware'=>"role:administrator"], function(){
Route::get('/admin', function(){
print "Administrators part here";
});
//some admin routes
});
$router->group(['middleware'=>"role:client"], function(){
Route::get('/client', function(){
print "Client part here";
});
//some client routes
});
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question