Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement service layer in Zend Framework? [closed]

I'm looking for some good resources to learn how to implement internal service layer in Zend Framework. This is interesting post Bookie Link, 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?
like image 896
takeshin Avatar asked Mar 16 '10 21:03

takeshin


3 Answers

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?)

like image 82
JamesG Avatar answered Oct 31 '22 12:10

JamesG


You don't need hacking to get service layer work. Default autoloader has a resource namespace Service_ with services folder inside application. So, it will load service layer from application\services, classes should follow Service_* naming pattern.

like image 42
umpirsky Avatar answered Oct 31 '22 13:10

umpirsky


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)

like image 2
Pascal MARTIN Avatar answered Oct 31 '22 13:10

Pascal MARTIN