精简路由组没有将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中重现这个问题,并且(可能)得到了和你一样的问题。

警告:{closure}()缺少参数1

group中的路由参数可以从它们的子参数中被放置到回调中:

$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);
    });
});

但是,是的,在每个路由中授权是很难看的,也许你可以在某种中间件中这样做?我认为文档页面上的最后一个看起来很适合你的需要。

编辑

哦,我最近发现了这个问题,@Gisheri已经得到了一个Slim维护者的答案。