【问题标题】:Restler 3 nested resources?Restler 3嵌套资源?
【发布时间】:2014-07-26 23:24:11
【问题描述】:

在restler中,可以使用嵌套资源吗?例如,对于restler,我会进行正常的/api/account/123 调用以获取该特定帐户。现在我想获取属于该帐户的客户。因此,我还想致电/api/account/123/client/456 以获取特定帐户的特定客户。

【问题讨论】:

标签: php rest restler


【解决方案1】:

您可以使用手动路由来定义此类路由。看下面的例子

use Luracast\Restler\RestException;

class Accounts
{

    /**
     * Get specific client for the given account
     *
     * @param int $id account id
     * @param int $client_id
     *
     * @throws RestException 404
     *
     * @return Client
     *
     * @url GET accounts/{id}/clients/{client_id}
     */
    public function getClient($id, $client_id)
    {
        $r = Client::where('account_id', '=', $id)->where('id', '=', $client_id)->firstOrFail();
        if (empty($r))
            throw RestException(404, 'Client is not found associated with the account');
        return $r;
    }

    /**
     * Get all clients associated with the given account
     *
     * @param int $id account id
     *
     * @return array {@type Client}
     *
     * @url GET accounts/{id}/clients
     */
    public function getClients($id)
    {
        return Client::where('account_id', '=', $id)->all();
    }

}

【讨论】:

  • 您能否改进资源管理器,使其显示返回类型以及抛出的异常?
  • 你试过 RC6 中的 Explorer 类了吗?
  • RC6? github页面说RC5
  • PHP 注意:第 266 行 .../vendor/Luracast/Restler/AutoLoader.php 中的数组到字符串转换以及 PHP 可捕获的致命错误:类 Closure 的对象无法转换为字符串.../vendor/Luracast/Restler/AutoLoader.php 第 266 行这是 PHP 5.4.24
  • 调用另一个 API 类的正确方法是什么?虽然我可以像上面显示的那样直接访问 Client 模型,但我的 Clients API 中可能已经有一个类可以做到这一点,并带有其他逻辑。所以我想从 Accounts API 调用 Clients API
猜你喜欢
  • 2011-09-26
  • 2011-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多