豁然高

导航

如何确定一个路由经过了那些中间件

主要从四个地方进行查看

1.查看\app\Http\Kernel.php中的全局路由。全局路由是所有路由都要经过的中间件

    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        // \App\Http\Middleware\TrustHosts::class,
        \App\Http\Middleware\TrustProxies::class,
        \Fruitcake\Cors\HandleCors::class,
        \App\Http\Middleware\PreventRequestsDuringMaintenance::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,

    ];

 

2.查看\routes中的路由文件。路由文件在定义路由的时候会同时指定中间件或者中间件组

Route::get('admin/profile', function () {
    //
})->middleware('auth');

Route::get('/', function () {
    //
})->middleware('first', 'second');

Route::get('admin/profile', function () {
    //
})->middleware(CheckAge::class);

Route::middleware(['auth','ip.limit'])->group(function () {
    Route::resource('user', App\Http\Controllers\UserController::class)->except(['show']);
});

 

3.查看路由服务提供者文件。 \app\Providers\RouteServiceProvider.php。这里可以为某些路由文件指定中间件或者中间件组。

路由文件中的所有路由都会经过指定的中间件或者中间件组

    public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::prefix('api')
                ->middleware('api')
                ->namespace($this->namespace)
                ->group(base_path('routes/api.php'));

            Route::middleware('web')
                ->namespace($this->namespace)
                ->group(base_path('routes/web.php'));
        });
    }

 

4.查看控制器中构造函数。构造函数中也可以指定中间件

class UserController extends Controller
{
    /**
     * 实例化一个新的控制器实例
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');

        $this->middleware('log')->only('index');

        $this->middleware('subscribed')->except('store');
    }
}

 

posted on 2022-07-14 10:50  豁然高  阅读(135)  评论(0编辑  收藏  举报