用于搜索的RESTful URL设计 [英] RESTful URL design for search
问题描述
我正在寻找一种合理的方式将搜索表示为RESTful URL。
I'm looking for a reasonable way to represent searches as a RESTful URLs.
设置:我有两个车型,汽车和车库,汽车可以在车库。所以我的网址看起来像:
The setup: I have two models, Cars and Garages, where Cars can be in Garages. So my urls look like:
/car/xxxx
xxx == car id
returns car with given id
/garage/yyy
yyy = garage id
returns garage with given id
汽车可以自己存在(因此/车),或者它可以存在于车库中。在一个给定的车库里,代表所有汽车的正确方法是什么?如下所示:
A Car can exist on its own (hence the /car), or it can exist in a garage. What's the right way to represent, say, all the cars in a given garage? Something like:
/garage/yyy/cars ?
车库yyy和zzz中汽车的联合如何?
How about the union of cars in garage yyy and zzz?
代表搜索具有某些属性的汽车的正确方法是什么?说:告诉我所有有4门的蓝色轿车:
What's the right way to represent a search for cars with certain attributes? Say: show me all blue sedans with 4 doors :
/car/search?color=blue&type=sedan&doors=4
或者应该是/ cars吗?
or should it be /cars instead?
使用搜索似乎不合适 - 有什么更好的方法/术语?应该是:
The use of "search" seems inappropriate there - what's a better way / term? Should it just be:
/cars/?color=blue&type=sedan&doors=4
如果搜索参数是PATHINFO或QUERYSTRING的一部分?
Should the search parameters be part of the PATHINFO or QUERYSTRING?
简而言之,我正在寻找一个用于跨模型REST URL设计和搜索的良好指南/教程。
In short, I'm looking for a good guide/tutorial for cross-model REST url design, and for search.
[更新]我喜欢Justin的答案,但他不涵盖多场搜索案例:
[Update] I like Justin's answer, but he doesn't cover the multi-field search case:
/cars/color:blue/type:sedan/doors:4
或类似的东西。我们如何从
or something like that. How do we go from
/cars/color/blue
到多个字段的情况?
推荐答案
为了搜索,使用查询字符串。这是完全RESTful的:
For the searching, use querystrings. This is perfectly RESTful:
/cars?color=blue&type=sedan&doors=4
定期查询字符串的一个优点是它们是标准的,被广泛的理解,并且可以从form-get生成。
An advantage to regular querystrings is that they are standard and widely understood and that they can be generated from form-get.
这篇关于用于搜索的RESTful URL设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!