Laravel中间件的使用

1.在route.php中添加中间件

Route::group(['middleware' => ['login']], function () {
    Route::get('/tt', 'IndexController@test');
});

2.在同级的Kernel.php中注册中间件

  protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'login' => \App\Http\Middleware\Login::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    ];

3.使用artisan命令生成中间件

php artisan make:middleware Login

4.在app/Http/Middleware文件夹下就会生成中间件类在其中添加拦截或过滤校验等处理即可

你可能感兴趣的:(Laravel中间件的使用)