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 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态