middleware
php artisan make:middleware isAdminMiddleware
user表增加is_admin字段 值为 Y/N
模型user.php
public function isAdmin(){
return $this->is_admin == 1;
}
修改isAdminMiddleware
public function handle($request, Closure $next)
{
info( $request->user());
if($request->user() && $request->user()->isAdmin()){
return $next($request);
}
return redirect('/');
}
web.php
Auth::loginUsingId(2);
Route::group(['prefix'=>'admin','middleware'=>'isAdmin'],function (){
Route::get('users',function (){
return 'admin only';
});
});
app/Http/Kernel.php
protected $routeMiddleware = [
'isAdmin' => isAdminMiddleware::class
];
}