代码之家  ›  专栏  ›  技术社区  ›  Seva Kalashnikov

排序优先级(流明)

  •  4
  • Seva Kalashnikov  · 技术社区  · 6 年前

    我在用 "laravel/lumen-framework": "5.7.*"

    我有两个中间商,第一个 AuthTokenAuthenticate bootstrap/app.php 喜欢

    $app->middleware([
        App\Http\Middleware\AuthTokenAuthenticate::class
    ]);
    

    $app->routeMiddleware([
        'auth.token' => Vendor\Utilities\Middleware\AuthToken::class
    ]);
    

    只适用于某些特定路线。

    我需要 auth.token 先执行,然后 AuthTokenAuthenticate 但我找不到方法,因为流明执行 $app->middleware

    拉威尔有 $middlewarePriority 这正是我所需要的,但我如何处理流明呢?

    0 回复  |  直到 6 年前
        1
  •  4
  •   cytsunny    5 年前

    我不认为这在流明是不可能的。我建议在 router group middleware options .


    删除全局中间件注册

    /引导/应用程序.php

    $app->middleware([
        //App\Http\Middleware\AuthTokenAuthenticate::class
    ]);
    

    /引导/应用程序.php

    $app->routeMiddleware([
        'auth.token' => Vendor\Utilities\Middleware\AuthToken::class,
        'auth.token.authenticate' => App\Http\Middleware\AuthTokenAuthenticate::class
    ]);
    

    auth.token.authenticate 一组同时拥有 auth.token .

    /路由/web/php

    $router->get('/', ['middleware' => 'auth.token.authenticate', function () use ($router) {
        // these routes will just have auth.token.authenticate applied
    }]);
    
    $router->get('/authenticate', ['middleware' => 'auth.token|auth.token.authenticate', function () use ($router) {
        // these routes will have both auth.token and auth.token.authenticate applied, with auth.token being the first one
    }]);
    

        2
  •  0
  •   The Unknown Dev    5 年前

    从LumenV6开始(可能更早),在定义路由时,可以将中间件指定为数组字段。在routes文件中 web.php ,我有如下内容:

    $router->get('/api/path/to/thing', [
        'uses' => 'FooController@index',
        'middleware' => ['etag', 'caching', 'bar']
    ]);
    

    middleware etag caching ,那么 bar ,按顺序排列。当只有一个中间件类时,可以将其指定为纯字符串,也可以将其指定为仅包含一个元素的数组。这当然可以扩展到 route groups