[英]Laravel 5.2 : How to get the role of current user when using `Zizaco/entrust`?
[英]Laravel 5.2 and `Zizaco/entrust` : How to control role's access to different path?
我正在使用Laravel 5.2和Zizaco/entrust
如何控制角色对不同路径的访问?
例如:
管理员的路由是这样的:
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::group(['prefix' => 'admin','namespace' => 'Admin'], function () {
Route::resource('dashboard', 'DashboardController');
});
});
我希望角色admin
可以访问http://www.example.com/admin/dashboard/
,
其他角色无法访问它,该怎么办?
您可以在中间件中检查管理员角色。 由于您正在使用zizaco / entrust,因此可以使用hasRole检查角色。
在中间件Web中使用此代码。
public function handle($request, Closure $next)
{
if (!$user->hasRole('admin')) {
return redirect('home');//Redirect to any page you wish.
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.