【问题标题】:API Explorer not using URL parametersAPI Explorer 不使用 URL 参数
【发布时间】:2012-10-11 17:09:35
【问题描述】:

我创建了一个 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 中输入:

http://[domain]/api/actions/available?meta=foobar

我得到了所需的输出:

“列表操作(元级别设置为 foobar)”

【问题讨论】:

    标签: php restler


    【解决方案1】:

    您使用上述代码所做的事情很少出现问题

    • 可选参数最好留给查询字符串
    • 当您使用 @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)";
        }
    
    }
    

    【讨论】:

    • 我的原始解决方案确实有效,只是在 API Explorer 中没有。我不确定我最初是否清楚这一点。我会更新问题以反映这一点。
    • 我也尝试了您建议的解决方案,但没有成功。它确实在 API Explorer 中创建了两个操作,但这些操作在 API Explorer 中都不起作用(这意味着它们总是返回“默认”值;将值放入提供的表单字段对结果没有影响)。与我的原始解决方案类似,您的解决方案在 API 资源管理器之外也可以工作。
    • 它对我来说很好,当我使用第二个操作时,我确实得到了正确的值,只是为了确保另一个 api 方法没有接听它的调用,首先评论其他人,然后如果它工作正常,然后更改方法顺序以使其工作。还要确保您使用的是最新版本的 Restler 3 和 Explorer
    • 好的,我试试看。顺便说一句,我的解决方案在技术上有什么问题吗?我有点喜欢文档更好的方式,因为它不会不必要地混淆可能不会经常使用的可选参数的服务方法的数量。在这种情况下,只有一个服务(但让参数工作)将是我理想的情况。无论如何,我会再次重试您的方法并发布结果。
    猜你喜欢
    • 1970-01-01
    • 2012-04-24
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    • 2020-05-03
    • 2013-03-22
    • 2019-05-20
    • 1970-01-01
    相关资源
    最近更新 更多