在restler中,是否可以使用嵌套资源?例如,对于restler,我会进行正常/api/account/123
调用以获取该特定帐户。现在我想获得属于该帐户的客户端。因此,我还想调用/api/account/123/client/456
来获取特定帐户的特定客户端。
答案 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();
}
}