Restler 3嵌套资源?

时间:2014-07-26 23:24:12

标签: php rest restler

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

1 个答案:

答案 0 :(得分:0)

您可以使用手动路由来定义此类路由。请参阅以下示例

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();
    }

}