Slim 3 - 从中​​间件访问 URI 参数

Slim 3 - Access URI arguments from middleware

我有一条路线,其中包含我捕获到 $args 变量中的参数:

$app->get('/v1/download/{transactionId}', function(Request $request, Response $response, $args) {   
...

我也在这条路线上使用中间件,我需要 $args 的内容可以从中间件功能访问。我确定我遗漏了一些简单的东西,但我似乎只能访问请求数据而不是参数。

非常感谢您的帮助!

谢谢!

你要的是documented here

如果您有权访问请求对象,那么您可以获得这样的路由参数:

$route = $request->getAttribute('route');
$courseId = $route->getArgument('transactionId');

编辑:您需要启用determineRouteBeforeAppMiddleware设置才能使其生效,因为默认情况下,路由在所有中间件执行后解析。

$app = new \Slim\App([
    'settings' => [
         ...
         // Set to true to be able to access the route from within the
         // middleware, otherwise it will return null
         'determineRouteBeforeAppMiddleware' => true,
         ...
    ]
]);