我正在尝试使用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?这不是路线组的重点吗?
答案 0 :(得分:0)
我试图重现这一点 - 使用最后一个稳定版本2.6.2 - 并且(可能)和你一样有问题。
警告:{closure}()
缺少参数1
组中的路线参数可以放在他们孩子的回调中:
$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维护者的答案。