Zend Framework REST service and client

Wednesday, July 16, 2008

Zend framework provides both client and service APIs for REST style implementations. One of the concerns that I have with the Zend_Rest_Service class is that, to use it properly with HTTP verbs, you have to use Zend_Controller. In other words, you have to use MVC model to implement a service properly. This is because, methods like isPost() that can be used to get to know the HTTP verb in use, are at controller layer.

Now, if you think of a service, there should not be MVC in there. You just have the service. It is the client that should use the MVC model and in place of using a database for the model, it should use a service. This is where you go form LAMP to LASP, where S stand for services, or SOA for that matter.

4 comments:

Sean said...

You can always turn off the view in the bootstrap or the controller:

// Configure Front Controller.
$frontController = Zend_Controller_Front::getInstance();
// Turn off the view renderer. $frontController->setParam('noViewRenderer', true);

Then MVC would be more of a MC. Where the model would deal with data from a database and the controller would control the logic depending on getParam() vars.

And you can just push the results to the screen with:

echo $results;

In a couple WS i've built i even offer the ability to get resultsets in JSON, php serialize or good old fashion XML.

ie:

echo serialize($results);
echo json_encode($results);

Samisa said...

I too have done this. However, my point is that, form the RestServer there is no way that we can get to know the HTTP verb in use, without the help of the controller. In REST, it is important to get to know if the request was a POST or a GET.

Seedninja said...

Actually a RESTful service completes all requirements for MVC model!

You saying REST shouldn't use MVC is like saying web pages shouldn't use images.

In RESTful app, the MVC portions are:

M)odel: What actually handles the data, and returns response
C)ontroller: Where you initiate proper RESTful service call, and return the data to user from model.
V)iew: The actual response is displayed right? There you go: The view.

But in ZF RESTful app your view is not your usual view, the view is handled by the Zend_Rest abstraction. However, you could just bypass Zend_Rest and manually output a response through normal view aswell, if you wanted to.

Seedninja said...

Oh and i forgot to say that RESTful app != LASP.

LAMP = Linux Apache MySQL PHP.
So you are proposing every RESTful app is Linux Apache Service PHP.

Sure you could use services as your sole data provider, but that's code side, not daemon/server side, thus bypassing RDBM would make the acronym LAP.

More to the point, it's simply stupid to use services only as your sole dataprovider, for a multitude of reasons, what if the service provider is down? What if you need to store data locally?

A providing service, like your case here, needs a reliable data source.

Nevermind the fact that why would people use your service, to get data from another service unless you enrich the data, or somehow add value. And again we are in data sources ...