V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
cevincheung
V2EX  ›  程序员

关于伪 RESTFul 的 URL 构造,好蛋疼,好纠结,好不淡定……

  •  
  •   cevincheung ·
    cevin · 2014-03-28 14:59:24 +08:00 · 3268 次点击
    这是一个创建于 3689 天前的主题,其中的信息可能已经有所发展或是发生改变。
    不罗嗦。蛋疼什么呢?


    比如订单吧:

    是 /orders/my
    还是 /my/orders

    比如微博吧
    是 /blogs/my
    还是 /my/blogs

    更详细的 /blogs/12312312312 /blogs/my/123123123
    比如还有 /blog/edit/23234234还是 /blog/123123/edit



    *天王盖地虎
    13 条回复    1970-01-01 08:00:00 +08:00
    zorceta
        1
    zorceta  
       2014-03-28 15:02:20 +08:00 via Android
    小鸡炖蘑菇

    个人倾向于/my/blogs

    这种语义化是矛盾的时候,按自己喜好选就好。
    用户又不会在意URL是吧(遁
    meepoli
        2
    meepoli  
       2014-03-28 15:12:30 +08:00
    我经常 给一个函数或一个变量名起名字要花上20分钟 然后5分钟就把需要写的写完了。。
    zencoding
        3
    zencoding  
       2014-03-28 15:40:01 +08:00
    /C/M
    lazygunner
        4
    lazygunner  
       2014-03-28 15:40:20 +08:00
    既然提到RESTFul, 我倾向于 users/<user_id>/orders/<order_id> 这种方案。
    至于 “/blog/123123/edit”, edit应该避免, 直接 PUT /blog/123123.
    XadillaX
        5
    XadillaX  
       2014-03-28 15:43:58 +08:00
    > 其实我也不知道,都是随性写的。
    johnsneakers
        6
    johnsneakers  
       2014-03-28 23:07:35 +08:00
    我是这样写的:
    [GET] http://yoursite.com/api/blog (returns all blogs)
    [GET] http://yoursite.com/api/blog/1 (returns blog with PK=1)
    [POST] http://yoursite.com/api/blog (create new blog)
    [PUT] http://yoursite.com/api/blog/1 (update blog with PK=1)
    [DELETE] http://yoursite.com/api/blog/1 (delete blog with PK=1)

    仅供参考
    mantianyu
        7
    mantianyu  
       2014-03-29 00:40:09 +08:00 via iPad
    一看lz就是 RESTful 规范文档没看仔细
    konakona
        8
    konakona  
       2014-03-29 01:17:24 +08:00
    看需求吧。
    是以功能为导向还是以途径为导向呢?

    以功能为导向则倾向于:blog/edit/123,blog/my/便于归纳功能。
    以途径为导向则倾向于:blog/123/edit,my/blog

    怎样的思考模式直接影响项目的走法,但并不影响最终的结果。
    cevincheung
        9
    cevincheung  
    OP
       2014-03-29 02:55:24 +08:00
    @lazygunner
    @mantianyu

    [ [伪] ] RESTFul。浏览器会执行put/delete的method么?
    ivanlw
        10
    ivanlw  
       2014-03-29 06:54:49 +08:00 via iPhone
    @mantianyu 求一份RESTful的规范文档行吗?
    mantianyu
        11
    mantianyu  
       2014-03-29 17:10:14 +08:00
    @cevincheung RESTful 不提倡 URI 里包含动词, 浏览器不支持 PUT/DELETE 的话, 在 querystring 里伪造


    @ivanlw google "RESTful tutorial"
    lazygunner
        12
    lazygunner  
       2014-03-30 21:33:31 +08:00
    @cevincheung 为什么一定要跟RESTful挂钩呢? 如果觉着RESTful约束太麻烦,定义自己风格的api就好了,不一定非要有个什么高大上的背景。
    yanke
        13
    yanke  
       2014-03-31 01:34:52 +08:00
    @cevincheung 可以用 Method Override 的方法实现。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1734 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 00:43 · PVG 08:43 · LAX 17:43 · JFK 20:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.