views:

58

answers:

2

Hi,
i googled for an hour now but maybe my Google-Fu is just too weak, i couldn't find a solution.

I want to create an application that queries a service via JSON requests (all data and backend/business logic is stored in the service). With plain PHP it's simple enough since i just make a curl request, json_decode the result and get what i need. This already works quite well.

A request might look like this:

Call http://service-host/userlist with body:

{"logintoken": "123456-1234-5678-901234"}

Get Result:

{
  "status": "Ok",
  "userlist":[
     {"name": "foo", "id": 1},
     {"name": "bar", "id": 2}
  ]
}

Now we want to get that into the Zend Framework since it's a hobby project and we want to learn about Zend. The problem is that all information i could find use a Database.

Is there even a way to create a Zend Project that does not use a Database? And how can i write a model that represents the actions instead of objects and object-relations?

+5  A: 

Have a look at Zend_Json and Zend_Http (or just use plain cURL).

As for your model, there should not be a difference whether the data source is a database or webservice. Just have one class that knows how to query the datasource. Whether the implementation of something like getUserById queries a database or a webservice is not important, e.g.

class UserGateway
{
    protected $_dataSource;
    public function __construct($dataSource)
    {
        $this->_dataSource = $dataSource;
    }
    public function getUserById($id)
    {
        // interact with datasource instance to retrieve a user by ID
    }
}

and then for $dataSource something like

class UserDb extends Zend_Db_Table {}

or something like

class UserWebService extends Zend_Http_Client {}

In other words, just create appropriate classes you can pass to the UserGateway, e.g.

$users = new UserGateway(new UserWebService);
$users->findById(123);

And exactly how you find the user then is an implementation detail in the Gateway and/or the data source class.

You might also be interested in Zend_Rest_Client and

Gordon
Thanks for the reply but those links don't really help much (Except for Zend_Http which might help) as they talk about building a REST Server with Zend but i want to access a REST-like Service from Zend. Still thanks for the effort.
dbemerlin
Zend_Http is just awesome. It sounds like you already have the service part from another website and you just want to make requests against that. If that is the case ignore Zend_Rest_Client.
Ballsacian1
The nice thing about Zend_Rest_Client is if the service your are thing to connect to, follows proper principles theoretically Zend_Rest_Client should handle making service calls in a nice fashion.
Ballsacian1
Thanks, your updates helped. I will dig into it further. Will wait for more answers before accepting though.
dbemerlin
+1  A: 

Since Zend Framework uses an MVC architecture (Model-View-Controller) the layers are easily interchangeable. Just write some classes that abstract you data sources and use them as your Model.

For example, I like to use Doctrine as a model layer in my projects instead of the table interface classes that come with ZF. Doctrine also does database abstraction, but you can substitute it with anything you like. It doesn't have to be database stuff.

Techpriester