Laravel 里kernel.php中三种middleware作用讲解

laravel框架学习

laravel目录下的app/Http目录中会有Kernel.php文件,里面会定有三种不同的middleware。

下面我么就说一下middleware 、middlewaregroups 、routeMiddleware 这三种中间件的作用

$middleware:全局中间件

所谓的全局中间件,就是中间件里的内容会全部执行

protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        \App\Http\Middleware\TrustProxies::class,
    ];

$middlewareGroups:分组中间件

只有在某个模块内才会调用,比如一般我们会定义三个web,admin,api

 

$routeMiddleware:路由中间件

内部中间件,只能在使用路由时候引用。

简单的举个例子

protected $routeMiddleware = [
        'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'admin.guest' => \App\Http\Middleware\RedirectIfAdminAuthenticated::class,//新添加后台跳转
    ];

在使用的时候就会自动跳转

 

 

白发渔樵

白发渔樵

发表评论

电子邮件地址不会被公开。 必填项已用*标注