dougao7801 2015-07-09 22:56
浏览 23
已采纳

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

I am trying to use slim's route groups, but I am having trouble getting the url parameters to pass anything in.

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

Why can't I access $id in the callback function? Isn't this the point of route groups?

  • 写回答

1条回答 默认 最新

  • duanrou5680 2015-07-10 06:53
    关注

    I've tried to reproduce this - with last stable release 2.6.2 - and got (probably) same problem like you.

    Warning: Missing argument 1 for {closure}()

    Route params in group could be placed in the callback from their childs:

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

    But yeah it's ugly to authorize in every single route, maybe you could do this in some kind of middleware? I think the last one on the docs page looks good to your needs.

    Edit

    Oh I recently found this issue, where @Gisheri already got an answer of a Slim maintainer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭