dtu1747 2016-09-22 14:59
浏览 84
已采纳

Slim Framework可选参数路由

I'm new to php and I'm using Slim Framework to develop a simple Rest Api.

According to docs, Slim uses FastRoute.

I have a route with optional params (vendor and quantity) like that:

path/items/{id}/{name}/{price}[/{vendor}[/{quantity}]]

The problem is that if I leave vendor blank the value of quantity goes to vendor and quantity will not be filled. I understand that with fast routes I can't achieve that. But, how can I do it?

  1. Can I do it with fast routes?
  2. Can I do it with Slim?
  3. Is there another framework that I can achieve what I want?
  4. Should I use only query strings? (I think that is the answer, but I need a help from someone with more knowledge).

Thank you.

  • 写回答

1条回答 默认 最新

  • douchiwan1503 2016-09-22 17:29
    关注

    If you are going to have optional parameters, which are of the same types and can be sent in any order, you could either use query strings, or have a fixed format where you add something like 0 instead of omitting it. Example where "vendor" isn't set:

    /10/somename/15/0/12
    

    Then the parameters aren't optional in the URL/route but you can handle that in your controller instead, just ignoring them if the param is a zero.

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

报告相同问题?

悬赏问题

  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站