[英]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.