在restler中,是否可以使用嵌套资源?例如,使用restler,我会执行普通的/api/account/123调用来获得特定的帐户。现在我想要那个帐户的客户。例如,我还想调用/api/account/123/client/456来获取特定帐户的特定客户端。
发布于 2014-07-28 03:35:38
您可以使用手动路由来定义此类路由。请参阅下面的示例
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();
}
}https://stackoverflow.com/questions/24976430
复制相似问题