简体   繁体   中英

Laravel auto run middleware for everypage without add Route::group in route

Route::group(['middleware' => ['web']], function(){

}

I have a middleware need to get user's point when everytime page load.

Is anyway to let this middleware run without add route group inside of route?

In your App\\Http\\Kernel.php file, add your middleware in $middleware array as:

/**
 * The application's global HTTP middleware stack.
 *
 * These middleware are run during every request to your application.
 *
 * @var array
 */
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\AppAfterMiddleware::class,
];

Docs

You can add your middleware in the app/Http/Kernel.php

If you add it to the property protected $middleware it will run for every request.

Instead added to the proterty protected $middlewareGroups it will run only for web or api requests.

The code in Kernel.php is commented and pretty self explanatory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM