我已经构建了我的第一个RESTful API并使用Slim作为我的框架.到目前为止它运作良好.
现在我已经看到一个很棒的API设计指南,它解释了,构建API的最佳方法是保持水平不变.我想这样做,并试图找出如何构建这样的URI:
my-domain.int/groups/search?q=my_query
Run Code Online (Sandbox Code Playgroud)
/ groups部分已经可以使用GET,POST,PUT,DELETE,搜索查询也可以这样工作:
my-domain.int/groups/search/my_query
Run Code Online (Sandbox Code Playgroud)
这是我在PHP中用于路由的代码:
$app->get('/groups/search/:query', 'findByName');
Run Code Online (Sandbox Code Playgroud)
我只是无法弄清楚如何在Slim中使用问号构建可选参数.我无法在谷歌上找到任何东西.
编辑:由于搜索似乎不适合我的场景我试图展示我想要实现的另一种方式:
假设我想从API获得部分响应.请求应如下所示:
my-domain.int/groups?fields=name,description
Run Code Online (Sandbox Code Playgroud)
不是这样的:
my-domain.int/groups/fields/name/description
Run Code Online (Sandbox Code Playgroud)
我怎么在路由中意识到这一点?
我正在为我们的网站开发一个搜索插件,允许用户使用 Google Places API 搜索任何位置,并列出我们的公寓及其到该位置的距离。
为了限制对用户的搜索,我向该方法添加了一些可能的限制:getPlacePredictions()可以在 Google Places API 对象中使用google.maps.places.AutocompleteService()。
国家:仅限德国
首选搜索区域:北莱茵-威斯特法伦州(德国的一个地区)
function pacGetSuggestions(searchString) {
// set autocomplete options
var pacOptions = {
input : searchString,
componentRestrictions : {country: 'de'}, // restrict results to germany
bounds : pacDefaultBounds, // add search area
};
// get suggestions
PAC.getPlacePredictions(pacOptions, pacDrawSuggestions);
}
Run Code Online (Sandbox Code Playgroud)
到目前为止效果很好!
我的问题:
如何通过特定位置类型限制结果?有什么办法吗?
例子:
我不希望用户能够搜索酒店/旅馆等,因为这对我们来说是直接竞争。
如果用户输入“Hil”代表“Hill-Street”或“HillyBilly Pizza Place”,那么她不应该显示“Hilton Hotels, Somethingstreet...”。
那可能吗?