在例9-3中,您了解了如何检查访问者是否已登录,如果没有则重定向。 您可以在应用程序的每个路径上执行这些类型的检查,但很快就会变得乏味。 事实证明,路由中间件(请参阅第10章了解有关它们如何工作的更多信息)非常适合限制访客或经过身份验证的用户的某些路由。
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
];
Example 9-8. Sample routes protected by auth middleware
Route::middleware('auth')->group(function () {
Route::get('account', 'AccountController@dashboard');
});
Route::get('login', 'Auth\LoginController@getLogin')->middleware('guest');