這是我的超級管理員中間件,
if(Auth::user()->role_id == 1) {
return $next($request);
}
return redirect('/');
而且,這里是管理中間件,
if(Auth::user()->role_id == 2) {
return $next($request);
}
return redirect('/');
這是我的經理中間件,
if(Auth::user()->role_id == 3) {
return $next($request);
}
return redirect('/');
這是我的賣家中間件,
if(Auth::user()->role_id == 4) {
return $next($request);
}
return redirect('/');
而且,這是 web.php,
Route::middleware(['superadmin'])->group(function () {
Route::resource('users', UserController::class);
//and more
});
Route::middleware(['admin'])->group(function () {
Route::resource('users', UserController::class);
//and more
});
Route::middleware(['manager'])->group(function () {
Route::resource('managers', ManagerController::class);
//and more
});
Route::middleware(['seller'])->group(function () {
Route::resource('sellers', SellersController::class);
//and more
});
我有 4 種中間件。每個路由組都有不同的路由。它作業正常。一些路線也用于另一組。但它不起作用。
uj5u.com熱心網友回復:
這里有兩個不同的中間件是沒有意義的。你想達到什么目標?如果用戶是管理員,那是什么,如果用戶是超級管理員,那又是什么?
如果目的只是允許管理員或超級管理員訪問定義在中間件組下的路由,那么一個中間件就足夠了。
//IsAdmin middleware
//if the user is either superadmin or admin allow else redirect
if(in_array(Auth::user()->role_id, [1,2])) {
//If you want to process based on whether admin or superadmin
//you can do it here
//if(Auth::user()->role_id ===1) {
// process when user is superadmin
// }
//else {
// process when user is admin
//}
return $next($request);
}
return redirect('/');
然后保護路由檔案中的路由
Route::middleware(['isAdmin'])->group(function () {
Route::resource('users', UserController::class);
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/485938.html
上一篇:嘗試登錄或注冊管理員、部署HostingerCpanel、本地主機作業正常時,找不到類“\App\Models\Admin”出現錯誤
