douguazhi5966 2016-01-03 23:47
浏览 38
已采纳

Slim Framework One路由不同的方法

I am trying to build a simple REST API for a dictionary App. I would like to have two GET methods that will get you the word by id and query(string). The problem is that the Slim framework routes everything through the first method and ignores the second one. I understand why it does it and I know you can use query string params but I am hoping there would be a way I can accomplish it this way. Thank you for your help.

http://localhost:5000/dictionary_api/words/1
$app->get('/words/:id', function($id) use ($app, $db) {
});


http://localhost:5000/dictionary_api/words/hello
$app->get('/words/:word', function($word) use ($app,$db){
});
  • 写回答

1条回答 默认 最新

  • dse84825 2016-01-03 23:55
    关注

    You can supply an array of conditions (regex matches) so that the route parameter will only match a certain format. Try the following

    $app->get('/words/:id', function($id) use ($app, $db)
    {
        //
    })->conditions(['id' => '[0-9]+']);
    

    This will make the :id parameter only match numeric values, anything else it shouldn't match and skip onto the next route.

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

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建