-- Don Bosco van Hoi <donbosco.van...@bigmouthmedia.com> wrote (on Monday, 27 July 2009, 03:57 PM -0700): > there are lots of nice approaches out there how to setup models and > databases and i really like the idea with the data mapper pattern as > described in the official zend framework quickstart tutorial. > > But what i am missing is also an example for webservices. How would you > setup a model to handle a soap/rest request? Is a Data Mapper also useful if > you only need to read data?
The answer to this is: write a Service Layer. The best way to describe a Service Layer is that it is the public API to your application: it represents all the discrete actions and behaviors that your application provides. You then write your MVC application to consume your Service Layer (which falls under the aegis of the Model); your controllers utilize the service objects, and pass results to the view. When you want to expose parts of your application via a web service, then, it becomes quite easy: simply write a class that proxies to parts of your service layer, and attach that class to one of the various server classes in Zend Framework: Zend_Soap_Server, Zend_XmlRpc_Server, Zend_Json_Server, Zend_Amf_Server. I went through some methods surrounding this both in my Dutch PHP Conference Zend Framework tutorial: http://www.slideshare.net/weierophinney/zend-framework-workshop-dpc09 as well as the CodeWorks webinar I did last week: http://www.slideshare.net/weierophinney/playdoh-modelling-your-objects-1766001 The benefits of this approach include easier and better approaches for unit testing, code re-use, the ability to use dependency injection, and much, much more. > For example i want to fetch some items from several shopping provider. Does > this one makes sense? Isnt this too much overhead? > > So what would be a good solution if you have different providers but always > want to return the same Item object? Add another method to > Default_Model_Item, for example fetchAllFromProvider2()? > > Thank you in advance for your replies. > > class Default_Model_Item > { > protected $_title; > protected $_price; > protected $_description; > > // getter setter > public function getXXX() > public function setXXX() > > public function find($id); > > // different providers > public function fetchAllFromProvider1($options); > public function fetchAllFromProvider2($options); > } > > class Default_Model_ItemMapper > { > public function find($id, $model); > > public function fetchAllFromProvider1($options) > { > $provider = new Default_Model_Webservice_ShoppingProvider1(); > $listings = $provider->makeCall($options); > foreach($listings as $item) > { > $entry = new Default_Model_Item(); > $entry->setTitle(); > $entry->setPrice(); > $entry->setDescription(); > } > } > > public function fetchAllFromProvider2($options) > { > $provider = new Default_Model_Webservice_ShoppingProvider2(); > $listings = $provider->makeCall($options); > foreach($listings as $item) > { > $entry = new Default_Model_Item(); > $entry->setTitle(); > $entry->setPrice(); > $entry->setDescription(); > } > } > > } > > class Default_Model_Webservice_ShoppingProvider1 { > public function makeCall($options) > } > > class Default_Model_Webservice_ShoppingProvider2 { > public function makeCall($options) > } > -- > View this message in context: > http://www.nabble.com/Zend-Model-with-Webservices-%28Soap-Rest%29-tp24689665p24689665.html > Sent from the Zend Framework mailing list archive at Nabble.com. > -- Matthew Weier O'Phinney Project Lead | matt...@zend.com Zend Framework | http://framework.zend.com/