Slim Route Group 未将 url 变量传递给回调

Slim Route Group not passing url variable to the callback

我正在尝试使用 slim 的路由组,但我无法获取 url 参数来传递任何内容。

$app->group('/contest/:id', function($id) use ($API){
    error_log('THIS IS THE CONTEST ID: '.$id); // id is blank... why?
    $API->authorize('contest', $id);
    $contest = new Contest($id);

    $app->get('', function() use ($contest, $API){
        $data = $contest->getSettings();
        $API->output($data);
    });

    $app->get('/settings', function() use ($contest, $API){
        $data = $contest->getSettings();
        $API->output($data);
    });

    $app->get('/stats', function() use ($contest, $API){
        $data = $contest->getStats();
        $API->output($data);
    });

    $app->get('/fields', function() use ($contest, $API){
        $data = $contest->getFields();
        $API->output($data);
    });
});

为什么我无法在回调函数中访问$id?这不是路由组的重点吗?

我已经尝试重现这个 - 使用最新的稳定版本 2.6.2 - 并且遇到了(可能)和你一样的问题。

Warning: Missing argument 1 for {closure}()

组中的路由参数可以放在他们的孩子的回调中:

$app->group('/contest/:id', function() use ($API){

    $app->get('', function($id) use ($contest, $API){
        $API->authorize('contest', $id);
        $contest = new Contest($id);
        $data = $contest->getSettings();
        $API->output($data);
    });
});

但是,是的,在每条路线上都授权是很丑陋的,也许你可以在某种 middleware 中做到这一点?我认为文档页面上的最后一个看起来很符合您的需要。

编辑

哦,我最近发现 this issue,@Gisheri 已经得到了 Slim 维护者的回答。