views:

761

answers:

2

I'm looking for some good resources to learn how to implement internal service layer in Zend Framework. This is interesting post, but with no concrete code samples.

  • Where to put service classes (/application/modules/modulename/services/?);
  • How to autoload them (custom autoloader?)
  • Most common services (user, authentication, cart, cache, feed?)
  • Sample implementations (any github repos?)
  • Good practices?
A: 

Basically, you could probably put those anywhere you like ; somewhere close to the model will most likely make sense, though.

As an example, you might want to take a look to :

  • ZFPlanet : an example of a planet developped with ZF
    • Not sure it's finished, but there are several classes, controllers, models, config files, ...
    • Which means that going through the code can help (it has, for me, for some things)
  • And, especially, its application/modules/zfplanet/models/Service directory
    • Which contains two classes.

(Well, I hope that's the sort of thing you meant by Service, actually)

Pascal MARTIN
Yes, I know ZFPlanet. It is a very good example to learn from. Paddy is still working on it. It is not finished yet, but working already. By service I mean something between model and controller.Now I put a lot code into models, and also use some custom classes, which I think may be called services.There are many types of models. But what's the difference between model and service actually?
takeshin
@takeshin See here http://martinfowler.com/eaaCatalog/serviceLayer.html - Everything inside the circle is the Model.
Gordon
Thanks, been there already. Have Fowlers books on 'must read' list.
takeshin
+3  A: 

I think the answer to this question depends on your needs, your time constraints and your overall approach to/style of software development.

I have recently (A) made the decision to use Zend Framework on a small but complex web application that has a very tight deadline and (B) have spent a LOT of time investigating ORM solutions and different ZF application structures in general. The conclusion I have come to is that there isn't a one-size-fits-all solution and that you should feel free to get creative and build an application structure that you are happy with.

If you have tight time constraints and the application isn't too large, then you could just create classes with names like Application_Model_BlahService and store them in the application/models directory and they will get picked up by default by the autoloader (assuming the autoloader has been bootstrapped correctly).

But if your application is larger or if, for some other reason, you want to split classes out into more directories, you could create your own sub-directories under the application directory and use something like the code below (which would exist in your application/Bootstrap.php) to add those classes to the autoloader:

protected function _initResourceLoader()
{
    $this->_resourceLoader->addResourceType( 'service', 'services', 'Service' );
    $this->_resourceLoader->addResourceType( 'serviceplugin', 'services/plugins', 'Service_Plugin' );
}

You can then create classes like Application_Service_Invoice, which would reside in application/services/Invoice.php and Application_Service_Plugin_TaxPlugin, which would reside in application/services/plugins/TaxPlugin.php. (Note: the code above assumes you are using Zend_Application).

You could, in theory, take this as far as you like and separate model classes from service classes from data access classes, etc etc etc. But again, it depends on the style of development that you prefer, the size of the team and, to some degree, what requirements your persistence layer imposes on you.

One last quick thing: have a look in Zend_Application_Module_Autoloader for a list of resources that are added to the autoloader by default. (Should I have mentioned that I'm referring to ZF 1.8+ in this answer?)

JamesG