問題描述
我正在尋找一些好的資源來學習如何在 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.php
和 Application_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中如何實現服務層?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!