Laravel 全局中间件的作用

<?php
namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    /**
     * 应用程序的全局 HTTP 中间件堆栈.
     * 这些中间件在应用程序的每个请求期间运行.
     * @var array
     */
    protected $middleware = [
        // 关于设置信任代理有关的中间件
        // @see https://learnku.com/docs/the-laravel-way/5.6/Tao-5-6/2947
        \App\Http\Middleware\TrustProxies::class,

        // 判断应用是否处于维护模式
        // @see https://learnku.com/docs/the-laravel-way/5.6/Tao-5-2/2943
        \App\Http\Middleware\CheckForMaintenanceMode::class,

        // 根据`php.ini`中的`post_max_size`的值对请求体的大小进行判断, 截取过大异常, 并抛出413异常
        // @see https://learnku.com/docs/the-laravel-way/5.6/Tao-5-3/2944
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,

        // 对`$_GET`和`$_POST`的数据进行前后空白字符清理
        // @see https://learnku.com/docs/the-laravel-way/5.6/Tao-5-4/2945
        \App\Http\Middleware\TrimStrings::class,

        // 将空字符串的字段转换为`null`
        // @see https://learnku.com/docs/the-laravel-way/5.6/Tao-5-5/2946
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
    ];

    /**
     * 应用程序的路由中间件租.
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'api' => [
            'throttle:60,1',
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
    ];

    /**
     * 应用程序的路由中间件.
     * 这些中间件可以分配给组或单独使用.
     * @var array
     */
    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,
        'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
        'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
    ];

    /**
     * 中间件的优先级排序列表.
     * 这将使非全局中间件始终按给定的顺序执行.
     * @var array
     */
    protected $middlewarePriority = [
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\Authenticate::class,
        \Illuminate\Routing\Middleware\ThrottleRequests::class,
        \Illuminate\Session\Middleware\AuthenticateSession::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
        \Illuminate\Auth\Middleware\Authorize::class,
    ];
}