有没有办法在执行操作的过程中执行REST操作?例如,如果我执行GET /index.php/book/1
,我可能会收到以下内容:
[{
"id" : 1,
"title" : "This is a book.",
"owner_id" : 4
}]
但是我想要做的是在返回上述对象之前,执行GET /index.php/user/4
以便最终结果是:
[{
"id" : 1,
"title" : "This is a book.",
"owner" : {
"id" : 4,
"name" : "John Smith",
"age" : 40
}
}]
答案 0 :(得分:1)
通过内部直接调用另一个api方法而不是浪费一次调用服务器,甚至可以通过Restler执行此操作的简单方法
class User{
public function get($id, $includeOwner = true){
$result = getUserFromDB($id)
if($includeOwner){
$result['owner'] = $this->get(getOwnerIdFromDB($id),false);
}
}
return $result;
}
HTH