如何在Zend Framework中实现服务层? [英] How to implement service layer in Zend Framework?

查看:57
本文介绍了如何在Zend Framework中实现服务层?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一些好的资源,以学习如何在Zend Framework中实现内部服务层. 这是有趣的帖子 Bookie Link ,但没有具体的代码示例.

  • 将服务类放在哪里(/application/modules/modulename/services/?);
  • 如何自动加载它们(自定义自动加载器?)
  • 大多数常用服务(用户,身份验证,购物车,缓存,供稿?)
  • 示例实现(任何github仓库?)
  • 好的做法?

解决方案

我认为这个问题的答案取决于您的需求,您的时间限制以及您对软件开发风格的总体了解.

我最近(A)决定在一个小型但复杂的Web应用程序上使用Zend Framework,该应用程序的截止日期非常紧迫,并且(B)花了很多时间研究ORM解决方案和总体上不同的ZF应用程序结构.我得出的结论是,没有一个万能的"解决方案,您应该自由发挥创造力,并构建满意的应用程序结构.

如果您有严格的时间限制并且应用程序不是太大,那么您可以创建名称类似于Application_Model_BlahService的类,并将它们存储在application/models目录中,默认情况下它们会被自动加载器拾取(假设自动加载器已正确引导).

但是,如果您的应用程序较大,或者由于某些其他原因想要将类拆分到更多目录中,则可以在应用程序目录下创建自己的子目录,并使用下面的代码(该代码会存在)在application/Bootstrap.php中)将这些类添加到自动加载器中:

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

然后,您可以创建类似Application_Service_Invoice的类,这些类将驻留在application/services/Invoice.phpApplication_Service_Plugin_TaxPlugin中,这些类将驻留在application/services/plugins/TaxPlugin.php中. (注意:上面的代码假定您使用的是Zend_Application.)

从理论上讲,您可以按照自己的喜好进行选择,并将模型类与服务类,数据访问类等分离开来.但是同样,这取决于您喜欢的开发方式,开发规模.团队,以及在某种程度上,您的持久层对您的要求是什么.

最后一件事:在Zend_Application_Module_Autoloader中查看默认情况下添加到自动装载器的资源列表. (我是否应该提到我在此答案中指的是ZF 1.8 +?)

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?

解决方案

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

这篇关于如何在Zend Framework中实现服务层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆