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 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?