問題描述
我正在尋找一些好的資源來學(xué)習(xí)如何在 Zend Framework 中實(shí)現(xiàn)內(nèi)部服務(wù)層.這是個(gè)有趣的帖子Bookie Link,但沒有具體的代碼示例.
- 在哪里放置服務(wù)類 (
/application/modules/modulename/services/
?); - 如何自動(dòng)加載它們(自定義自動(dòng)加載器?)
- 最常見的服務(wù)(用戶、身份驗(yàn)證、購物車、緩存、提要?)
- 示例實(shí)現(xiàn)(任何 github 存儲(chǔ)庫?)
- 好的做法?
我認(rèn)為這個(gè)問題的答案取決于您的需求、您的時(shí)間限制以及您的軟件開發(fā)的整體方法/風(fēng)格.
我最近 (A) 決定在一個(gè)小型但復(fù)雜的 Web 應(yīng)用程序上使用 Zend Framework,該應(yīng)用程序的期限非常緊迫,并且 (B) 花了大量時(shí)間研究 ORM 解決方案和不同的 ZF 應(yīng)用程序結(jié)構(gòu).我得出的結(jié)論是,沒有一刀切的解決方案,您應(yīng)該盡情發(fā)揮創(chuàng)意并構(gòu)建一個(gè)您滿意的應(yīng)用程序結(jié)構(gòu).>
如果時(shí)間緊迫且應(yīng)用程序不是太大,那么您可以創(chuàng)建名稱類似于 Application_Model_BlahService
的類并將它們存儲(chǔ)在 application/models
目錄,默認(rèn)情況下它們會(huì)被自動(dòng)加載器選取(假設(shè)自動(dòng)加載器已正確引導(dǎo)).
但是,如果您的應(yīng)用程序較大,或者由于某些其他原因,您想將類拆分到更多目錄中,則可以在應(yīng)用程序目錄下創(chuàng)建自己的子目錄,并使用類似于下面的代碼(可能會(huì)存在)在您的 application/Bootstrap.php
) 中將這些類添加到自動(dòng)加載器:
受保護(hù)的函數(shù) _initResourceLoader(){$this->_resourceLoader->addResourceType('service', 'services', 'Service');$this->_resourceLoader->addResourceType('serviceplugin', 'services/plugins', 'Service_Plugin');}
然后您可以創(chuàng)建諸如 Application_Service_Invoice
之類的類,它們將駐留在 application/services/Invoice.php
和 Application_Service_Plugin_TaxPlugin
中,它們將駐留在application/services/plugins/TaxPlugin.php
.(注意:上面的代碼假設(shè)您使用的是 Zend_Application
).
理論上,您可以隨心所欲地將模型類與服務(wù)類與數(shù)據(jù)訪問類等分開.但同樣,這取決于您喜歡的開發(fā)風(fēng)格、應(yīng)用程序的大小團(tuán)隊(duì),在某種程度上,你的持久層對(duì)你有什么要求.
最后一件事:查看 Zend_Application_Module_Autoloader
中默認(rèn)添加到自動(dòng)加載器的資源列表.(我應(yīng)該在這個(gè)答案中提到我指的是 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?)
這篇關(guān)于Zend Framework中如何實(shí)現(xiàn)服務(wù)層?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!