Laravel middleware

Vckin發表於2020-04-20

1,建立中介軟體

php artisan make:middleware GetMenu 

2,Kernel.php 註冊中介軟體

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,
        'get.menu' => \App\Http\Middleware\GetMenu::class,
    ];

3,在路由中指定中介軟體


Route::group(['middleware' => ['get.menu']],function(){
    Route::get('/', function () {
        return view("welcome");
    })->name("admin_index");
});

Route::get('/', function () {
    return view("welcome");
})->middleware("get.menu");
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章