문제
작업을 실행하는 도중에 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
}
}]
해결책
서버에 대한 호출을 낭비하는 대신 내부적으로 다른 API 메소드를 직접 호출하여 다시 다른 API 메소드를 호출하는 데 간단한 방법이 있습니다
class User{
public function get($id, $includeOwner = true){
$result = getUserFromDB($id)
if($includeOwner){
$result['owner'] = $this->get(getOwnerIdFromDB($id),false);
}
}
return $result;
}
.
hth
제휴하지 않습니다 StackOverflow