PHP Classes

RestAction - implementation example needed

Recommend this page to a friend!

      REST Server  >  All threads  >  RestAction - implementation example...  >  (Un) Subscribe thread alerts  
Subject:RestAction - implementation example...
Summary:Can you provide an example of a RestAction implementation
Messages:5
Author:Ignatius Teo
Date:2009-04-20 05:24:26
Update:2009-04-21 00:21:49
 

  1. RestAction - implementation example...   Reply   Report abuse  
Picture of Ignatius Teo Ignatius Teo - 2009-04-20 05:24:27
In the RestServer::call method you have:
<code>
if($class instanceof RestAction) return $this->call($class);
</code>

I don't see how this could work without the script entering an infinite loop. Because, everytime you initiate a RestAction (or a subclass which is an instance of RestAction), it will call RestServer::call infinitely.

Can you provide an example of a subclassed RestAction and how it would work?



  2. Re: RestAction - implementation example...   Reply   Report abuse  
Picture of Diogo Souza da Silva Diogo Souza da Silva - 2009-04-20 14:06:49 - In reply to message 1 from Ignatius Teo
RestAction implementations are the RestController and RestView interfaces, it represents a possible handler for the Request.

$class will be the return of a RestController or a RestView (both are RestAction), this test is to see if any of those return'd another RestAction( controller or view) that should be called later.

When a request reach it end, or if there should be no more classes to call, the current action should return the RestServer.

In example2, controller/HomeController method execute(RestServer) return an instance of HomeView. This is where this code will work, as HomeView implements RestView, witch is a RestAction, the RestServer will proceed the request with HomeView show(RestServer) method.

Them as HomeView show() do not return a RestAction(it returns the RestServer), the RestServer ends the request and show the headers and output.

It would loop infinite if the controller/view method return it self or the next class return it's caller.

  3. Re: RestAction - implementation example...   Reply   Report abuse  
Picture of Ignatius Teo Ignatius Teo - 2009-04-20 22:23:53 - In reply to message 2 from Diogo Souza da Silva
Ah, I see now... so you can chain RestActions. I understand how the RestViews and RestControllers work, because you explicitly test for these at the start of the RestServer::call method.

Maybe it would be better to do this to prevent an infinite loop?

if($class instanceof RestAction
&& get_class($this) != get_class($class)) {
return $this->call($class); // May have another class to follow the request
}



  4. Re: RestAction - implementation example...   Reply   Report abuse  
Picture of Diogo Souza da Silva Diogo Souza da Silva - 2009-04-21 00:00:11 - In reply to message 3 from Ignatius Teo
As you sugested, I changed the code to prevent infinite loop, but I did it by starting a simple stack trace of calls.

An alternative is the following call method:

private function call($class,$method=null) {
$lastClass = get_class($class) ;
if($class instanceof RestView) {
if($method==null) $method="show";
$class = $class->$method($this) ;
} else if($class instanceof RestController) {
if($method==null) $method="execute";
$class = $class->$method($this);
}

if($class instanceof RestAction
&& get_class($class) != $lastClass ) {
return $this->call($class);
}

return $this ;
}

The diference it that when calling get_class($this) would return "RestServer", so i saved the class just called.

  5. Re: RestAction - implementation example...   Reply   Report abuse  
Picture of Ignatius Teo Ignatius Teo - 2009-04-21 00:21:49 - In reply to message 4 from Diogo Souza da Silva
Yep, thanks. That'll work.