Question

Is there a way to perform REST actions in the middle of executing an action? For example, if I perform GET /index.php/book/1 I might receive the following:

[{
    "id" : 1,
    "title" : "This is a book.",
    "owner_id" : 4
}]

But what I'd like to do is before returning the above object, perform a GET /index.php/user/4 so the end result is:

[{
    "id" : 1,
    "title" : "This is a book.",
    "owner" : {
        "id" : 4,
        "name" : "John Smith",
        "age" : 40
    }
}]
Was it helpful?

Solution

There is even simple way of doing this with Restler by internally calling another api method directly instead of wasting one call to the server

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

HTH

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top