用于搜索的 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
汽车可以单独存在(因此是/car),也可以存在于车库中.例如,表示给定车库中的所有汽车的正确方法是什么?类似的东西:
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 guidance for cross-model REST url design, and for search.
[更新] 我喜欢贾斯汀的回答,但他没有涵盖多字段搜索案例:
[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屋!