其他分享
首页 > 其他分享> > 如何确定一个路由经过了那些中间件

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

作者:互联网

主要从四个地方进行查看

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');
    }
}

 

标签:function,Http,middleware,中间件,确定,class,路由
来源: https://www.cnblogs.com/gaoBlog/p/16476831.html