Laravel 中间件

创建中间件

php artisan make:middleware PrintHelloworld

自动生成了 middleware 文件:app\Http\Middleware\PrintHelloworld.php

修改中间件文件

app\Http\Middleware\PrintHelloworld.php

注册中间件

修改 app\Http\kernel.php 文件;有全局注册、grand注册、路由注册三种方式;

全局注册:作用于所有路由

.
.
.
    protected $middleware = [
        \App\Http\Middleware\TrustProxies::class,
        \Fruitcake\Cors\HandleCors::class,
        \App\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,

        \App\Http\Middleware\PrintHelloworld::class,
    ];
.
.
.

访问主页, dump 语句会输首先输出。

路由注册:作用于注册的路由

修改 app\Http\kernel.php 中的 protected $routeMiddleware部分;

.
.
.
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,

        'printinfo' =>  \App\Http\Middleware\PrintHelloworld::class,
    ];
.
.
.

指定单个路由

修改 routes\web.php 文件

middleware('printinfo');

这样,这个中间件只会在访问指定路由时起作用;

指定一组路由

修改 routes\web.php 文件

middleware('printinfo');


也可以在控制器中的 construct 函数中调用

public function __construct()
{
    //这种方式会对该控制器里的所有方法都产生作用
    //$this->middleware('printinfo);  
          
    //这种方式指定对控制器里的某个方法产生作用
    $this->middleware('checklog', [
        'only' => ['edit', 'update']
    ]);
    
    //这种方式排除控制器里的某个方法
    $this->middleware('checklog', [
        'except' => ['edit', 'update']
    ]);
    
}

作用于 grand

修改 app\Http\kernel.php 中的 protected $middlewareGroups部分;

.
.
.

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,
            \App\Http\Middleware\PrintHelloworld::class,
        ],

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

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