我创建了一个 RESTful API,其中有一个作为 URL 变量传入的可选参数。当直接从浏览器执行时,它似乎可以正常工作,但是当在 API Explorer 中尝试它时,它会列出参数,但在执行时会忽略它。我不知道从哪里开始解决这个问题。任何帮助将不胜感激。
类定义如下:
class actions {
/**
* LIST available Actions
*
* List all the actions that a user (or app) can choose from. The response list
* will include [name],[slug/id], and [description] attributes. If you want a more complete set of
* meta attributes for the actions then you can specify "meta=all" in the request url. For full spec of
* response please review LG_actions_list.json.
*
* @url GET /available
*
* @param $meta {@from url} Optional parameter to control the amount of meta-data passed back. Values are "none","normal", and "all"
**/
public function available ($meta="normal")
{
return "list actions (meta level set to $meta)";
}
}
在这种情况下,我可以在 API 资源管理器中输入“all”作为 $meta 的值,但响应仍然是“列出操作(元级别设置为正常)”。
更新:
为了阐明此行为,我添加了 API Explorer 输出以及直接调用服务时获得的输出:
相比之下,实际使用 API 时我得到了正确的结果。在 Chrome 中输入:
我得到了所需的输出:
"list actions (meta level set to foobar)"
最佳答案
您使用上述代码所做的事情几乎没有问题
- 可选参数最好留给查询字符串
- 当您使用
@url
添加手动路由时,不会为该方法添加自动路由
执行以下操作使其正常工作。
- 将方法名称更改为
get
以将其映射到类的根 - 关闭智能自动路由
现在在资源管理器中将列出两个操作
actions.json
actions.json/{meta}
.
class actions {
/**
* LIST available Actions
*
* List all the actions that a user (or app) can choose from. The response list
* will include [name],[slug/id], and [description] attributes. If you want a more complete set of
* meta attributes for the actions then you can specify "meta=all" in the request url. For full spec of
* response please review LG_actions_list.json.
*
* @smart-auto-routing false
* @param $meta Optional parameter to control the amount of meta-data passed back. Values are "none","normal", and "all"
**/
public function get ($meta="normal")
{
return "list actions (meta level set to $meta)";
}
}
关于php - API Explorer 不使用 URL 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12844909/