質問

アクションを実行する途中で休憩行為を実行する方法はありますか?たとえば、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メソッドを直接呼び出すことで、これをリターンでも簡単な方法があります。

class User{
    public function get($id, $includeOwner = true){
        $result = getUserFromDB($id)
        if($includeOwner){
            $result['owner'] = $this->get(getOwnerIdFromDB($id),false);
        }
    }
    return $result;
}
.

hth

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top