Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend without Database

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?


1 Answers

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

  • Building RESTful Services with Zend Framework
  • Getting started with REST using Zend Framework
  • RESTful Web Services with Zend Framework
like image 91
Gordon Avatar answered Mar 11 '26 00:03

Gordon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!