我正在使用laravel中的中間件。我有兩個中間件,一個是管理員,另一個是專員如何在laravel中爲多種類型的管理員使用中間件?
現在,在這兩個中間件中,一些路由訪問這兩個中間件,有些則不是。現在發生什麼事情是我想讓管理中間件的個人路由不能在委員中間件中被訪問。 在這裏,我曾嘗試: -
//Admin Middleware Route
Route::group(["middleware" => ['admin']], function() {
Route::match(['get', 'post'], '/admin/users', '[email protected]');
});
//Commissioner Middleware Route
Route::group(["middleware" => ['commissioner']], function() {
//we can put later on these routes
});
// common middleware routes between commissioner and admin
Route::group(["middleware" => ['admin','commissioner']], function() {
Route::match(['get', 'post'], '/admin/dashboard', '[email protected]');
Route::match(['get', 'post'], '/admin/profile', '[email protected]');
});
現在,當我訪問AdminController @用戶路線,當我通過專員登錄就可以訪問,但我想這條路線不是在當局長登錄訪問。但AdminController @儀表盤和AdminController @輪廓應該可以訪問兩個中間件
When admin login then type is : master
when commsioner login then type is : commissioner
// Commissioner Middleware
class Commissioner
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if(empty(Session::has('adminSession'))){
return redirect()->action('[email protected]')->with('flash_message_error', 'Please Login');
}
return $next($request);
}
}
// admin Middleware
class Admin
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if(empty(Session::has('adminSession'))){
return redirect()->action('[email protected]')->with('flash_message_error', 'Please Login');
}
return $next($request);
}
}
請幫助我,我現在用laravel 5.2。感謝advnace :)
爲什麼你的專員中間件檢查「Session :: has('adminSession')」,這是一個錯字?它應該是「Session :: has('commissionerSession')」嗎? – yixiang
不,這是管理員因爲commsioner也保存在管理表中,並有不同的類型 – kunal