1. 程式人生 > >Laravel 中介軟體建立使用

Laravel 中介軟體建立使用

建立中介軟體
要建立一個新的中介軟體,可以使用
 make:middleware 這個 Artisan 命令:

PHP
 artisan make:middleware privilege(中介軟體名稱)
註冊中介軟體
若是希望中介軟體被所有的
 HTTP 請求給執行,只要將中介軟體的類加入到 app/Http/Kernel.php 的 $middleware 屬性清單列表中。
protected $middleware = [
        \App\Http\Middleware\RequestLog::class,
        \Illuminate\Foundation
\Http\Middleware\CheckForMaintenanceMode::class, \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, ]
; protected $routeMiddleware = [ 'auth' => \App\Http\Middleware\Authenticate::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, //add token middleware 'jwt.auth' => \Tymon
\JWTAuth\Middleware\GetUserFromToken::class, 'jwt.refresh' => \Tymon\JWTAuth\Middleware\RefreshToken::class, //ability middleware 'ability' =>\App\Http\Middleware\AbilitiesMiddleware::class, //inner server sign 'server.sign' => \App\Http\Middleware\ServerAuthMiddleware::class ];