php - 流明中间件排序(优先级)
问题描述
我在用着"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
我找不到方法,因为 Lumen$app->middleware
先执行路由。
Laravel 有$middlewarePriority
这正是我需要的,但我如何在 Lumen 中处理它?
解决方案
我认为这在 Lumen 中不可能以您想要的方式实现。我建议将中间件与路由器组中间件选项一起使用。
删除全局中间件注册
/bootstrap/app.php
$app->middleware([
//App\Http\Middleware\AuthTokenAuthenticate::class
]);
将两个中间件添加到路由中间件
/bootstrap/app.php
$app->routeMiddleware([
'auth.token' => Vendor\Utilities\Middleware\AuthToken::class,
'auth.token.authenticate' => App\Http\Middleware\AuthTokenAuthenticate::class
]);
创建两个路由组:一个只包含auth.token.authenticate
一个组,一个包含两个auth.token
和 auth.token.authenticate
一个组。
/routes/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
}]);
我认为这是获得预期效果的最干净的方法。
推荐阅读
- node.js - Azure cosmosdb Mongo Api 身份验证错误
- pagespeed - pagespeed 见解推迟未使用的 CSS 和屏幕图像
- google-chrome-extension - 我可以在不请求的情况下从扩展程序发出请求吗
允许? - websphere - WASX7129E: 无法在“DescriptiveProperty”类型的父项中创建“Property”类型的对象
- python - 如何在我的 django 工作中在 mongoengine 中搜索 null 或 none?
- dns - 使用主机名但 IP 无法访问 PC
- ruby-on-rails - 如何使用 HTTParty gem 与 Rails5 中的外部搜索 API 交互?
- python - 将数据从字典加载到 MultiIndex 数据框中
- excel - 表 1 中的 VBA 数据与 A 列表 2 中的任何单元格匹配
- angular - 如何在 Angular 路由中使用数组?