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

查看:16
本文介绍了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) 中将这些类添加到自动加载器:

受保护的函数 _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天全站免登陆