【问题标题】:Slim 3 - Access URI arguments from middlewareSlim 3 - 从中​​间件访问 URI 参数
【发布时间】:2018-07-19 08:40:19
【问题描述】:

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

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

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

非常感谢您的帮助!

谢谢!

【问题讨论】:

    标签: php slim


    【解决方案1】:

    你要的是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,
             ...
        ]
    ]);
    

    【讨论】:

    • 我认为您需要先启用determineRouteBeforeAppMiddleware 设置才能工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    相关资源
    最近更新 更多