URL设计在宁静的API的结果高级过滤 [英] URL design for advanced filtering of results in restful API
问题描述
让我们来你可能内置了PHP宁静的API模型资源的一个普通例子。
Let's take a generic example of resources you might model with a restful API built in PHP.
- 一个车库拥有多辆,多机制。
- 一辆车只有一个车库。
- 机械师拥有多辆,多的车库(他是确定一个工作狂!)。
所以逻辑上说,我们的终点应该是
So logic says that our endpoints should be
/v1/garages
/v1/garages/1234
/v1/mechanics
/v1/mechanics/1234
/v1/cars
/v1/cars/1234
如果我们想获得车库1234年,我们将使用所有汽车 GET / V1 /车库/ 1234 /汽车
If we wanted to get all cars of garage 1234 we would use
GET /v1/garages/1234/cars
如果我们希望所有辆机械12在车库34我们将使用 GET / V1 /机械/ 12 /车库/ 34 /汽车
If we wanted all cars for mechanic 12 that are in garage 34 we would use
GET /v1/mechanics/12/garage/34/cars
但怎么样,如果我们希望所有的汽车从车库56和78?结果
我有一些想法记住,但我能想到的最好的是 GET / V1 /汽车?车库= [ID = [56,78]
But what about if we wanted all cars from garages 56 AND 78?
I have a few ideas in mind but the best I can think of is
GET /v1/cars?garage=[id=[56,78]]
这将使我们能够抓住所有的绿色汽车行驶两个或四个门从具有通过黄色油漆和碳帽子的车库 GET / V1 /汽车车库= [漆=黄,引擎盖= [材料=碳]&放大器;颜色=绿色和放大器;门= [2,4]
Which would allow us to grab all green cars with two or four doors from any garages that have yellow paint and carbon bonnets via
GET /v1/cars?garage=[paint=yellow,bonnet=[material=carbon]]&color=green&doors=[2,4]
这个问题似乎是一个合理的主意还是我去这个越来越复杂的嵌套遇到问题,这应该是很容易用PHP解析?
Does this seem like a reasonable idea or am I going to run into issues with this getting to complex with the nesting, should this be easy enough to parse with PHP?
另一个想法我是JSON作为据我所知,所有JSON是在URL中,以便有效
Another idea I had was Json as far as I am aware all json is valid in a url so
GET /v1/cars?{\"color\":\"green\",\"doors\":[2,4],\"garage\":{\"paint\":\"yellow\",\"bonnet\":{\"material\":\"carbon\"}}}$c$c>
这使得它非常便于开发人员使用,然后,因为几乎所有的编程语言现在有JSON支持,并在API结束时,我们只需要使用 json_de code($ _ SERVER ['QUERY_STRING ])
那么,有没有使用任何问题的Json?
Which makes it super easy for developers to use then as almost all programming languages now have json support, and on the API end we can just use json_decode($_SERVER[''QUERY_STRING])
so are there any issues with using Json?
第三个想法创建搜索
我的意思是,我们使用
POST /汽车/搜索
上述JSON作为后数据的请求将直接返回搜索ID到的结果然后可以通过获取 GET /汽车/搜索/ {ID}
这将减轻我身边的复杂性和避免请求8KB的限制(这是一个API,并不意味着通过浏览器的地址栏使用) 。但我担心,如果我这样做,那么就违背了顶级的目的 GET / V1 /汽车
终点?
POST /cars/search
with the above Json as post data that request will simply return the ID of the search to which the results could then be fetched via GET /cars/search/{id}
this would alleviate the complexity on my side and avoid the 8kb limit for requests (this is an API, not meant to be used by browser address bars). but I am concerned that if I do this then it defeats the purpose of the top level GET /v1/cars
endpoint?
推荐答案
由于在 #laravel IRC频道乐于助人的家伙了谁我宁愿延长谈话,我们有似乎工作答案。
Thanks to the helpful guys over at the #laravel irc channel who I had rather an extended conversation with we have an answer that seems to work.
我们选择了基于一些事情这样的回答:
We chose this answer based on a few thing:
- 易读易于
- 查询功率
- 平坦度(解析易于)
- 过滤易于
的基本要求是
GET /v1/cars?color=green
&doors=2,4
&garage[paint]=yellow
&garage[bonnet][material]=carbon
其中当与以下code组合
Which when combined with the following code
$query = array();
parse_str($_SERVER['QUERY_STRING'], $query);
吐出像下面这可以在API中很容易地遍历数组做过滤:)
Spits out an array like below which can be easily iterated over in the API to do the filtering :)
Array
(
[color] => green
[doors] => 2,4
[garage] => Array
(
[paint] => yellow
[bonnet] => Array
(
[material] => carbon
)
)
)
这篇关于URL设计在宁静的API的结果高级过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!