2

アクションの実行中に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
    }
}]
4

1 に答える 1

1

サーバーへの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

于 2012-10-04T13:18:59.950 に答える