問(wèn)題描述
我正在一個(gè)現(xiàn)有項(xiàng)目中實(shí)施 Zend Framework,該項(xiàng)目有一個(gè)公共營(yíng)銷(xiāo)區(qū)、一個(gè)私有成員區(qū)、一個(gè)管理站點(diǎn)和一個(gè)營(yíng)銷(xiāo)活動(dòng)管理站點(diǎn).目前,營(yíng)銷(xiāo)區(qū)域和成員區(qū)域的控制器腳本都在網(wǎng)站的根目錄下,然后是單獨(dú)的 admin 文件夾和營(yíng)銷(xiāo)活動(dòng)網(wǎng)站的另一個(gè)文件夾.
I am working on implementing Zend Framework within an existing project that has a public marketing area, a private members area, an administration site, and a marketing campaign management site. Currently these are poorly organized with the controller scripts for the marketing area and the members area all being under the root of the site and then a separate folder for admin and another folder for the marketing campaign site.
在實(shí)施 Zend 框架時(shí),我希望能夠?qū)⒖刂破骱鸵晥D拆分為模塊(一個(gè)用于成員區(qū)域,一個(gè)用于公共營(yíng)銷(xiāo)區(qū)域,一個(gè)用于管理站點(diǎn),一個(gè)用于營(yíng)銷(xiāo)活動(dòng)管理站點(diǎn)),但我需要能夠?qū)⒚總€(gè)模塊指向同一個(gè)模型,因?yàn)樗腥齻€(gè)組件都在同一個(gè)數(shù)據(jù)庫(kù)和同一個(gè)業(yè)務(wù)對(duì)象上工作.
In implementing the Zend Framework, I would like to create be able to split the controllers and views into modules (one for the members area, one for the public marketing area, one for the admin site, and one for the marketing campaign admin site) but I need to be able to point each module to the same model's since all three components work on the same database and on the same business objects.
但是,我無(wú)法在文檔中找到有關(guān)如何執(zhí)行此操作的任何信息.任何人都可以提供有關(guān)如何執(zhí)行此操作的鏈接或有關(guān)如何完成此操作的一些簡(jiǎn)單說(shuō)明的幫助嗎?
However, I haven't been able to find any information on how to do this in the documentation. Can anyone help with either a link on how to do this or some simple instructions on how to accomplish it?
推薦答案
我所做的是將通用類(lèi)保存在模塊層次結(jié)構(gòu)之外的庫(kù)"目錄中.然后設(shè)置我的 INCLUDE_PATH
使用相應(yīng)模塊的models"目錄,加上公共的library"目錄.
What I do is keep common classes in a "library" directory outside of the modules hierarchy. Then set my INCLUDE_PATH
to use the "models" directory of the respective module, plus the common "library" directory.
docroot/
index.php
application/
library/ <-- common classes go here
default/
controllers/
models/
views/
members/
controllers/
models/
views/
admin/
controllers/
models/
views/
. . .
在我的引導(dǎo)腳本中,我將application/library/
"添加到INCLUDE_PATH
.然后在每個(gè)控制器的 init()
函數(shù)中,我將該模塊的models/
"目錄添加到 INCLUDE_PATH
.
In my bootstrap script, I'd add "application/library/
" to the INCLUDE_PATH
. Then in each controller's init()
function, I'd add that module's "models/
" directory to the INCLUDE_PATH
.
edit:setControllerDirectory()
和 setModuleDirectory()
等函數(shù)不會(huì)將各自的模型目錄添加到 INCLUDE_PATH代碼>.無(wú)論如何,您必須自己執(zhí)行此操作.以下是如何操作的一個(gè)示例:
edit: Functions like setControllerDirectory()
and setModuleDirectory()
don't add the respective models directories to the INCLUDE_PATH
. You have to do this yourself in any case. Here's one example of how to do it:
$app = APPLICATION_HOME; // you should define this in your bootstrap
$d = DIRECTORY_SEPARATOR;
$module = $this->_request->getModuleName(); // available after routing
set_include_path(
join(PATH_SEPARATOR,
array(
"$app{$d}library",
"$app{$d}$module{$d}models",
get_include_path()
)
)
);
您可以在引導(dǎo)程序中將library
"添加到您的路徑中,但是您不能在引導(dǎo)程序中為正確的模塊添加models
"目錄,因?yàn)槟K依賴(lài)于路由.有些人在他們的控制器的 init()
方法中這樣做,有些人為 ActionController 的 preDispatch 鉤子編寫(xiě)了一個(gè)插件來(lái)設(shè)置 INCLUDE_PATH
.
You could add the "library
" to your path in the bootstrap, but you can't add the "models
" directory for the correct module in the bootstrap, because the module depends on routing. Some people do this in the init()
method of their controllers, and some people write a plugin for the ActionController's preDispatch hook to set the INCLUDE_PATH
.
這篇關(guān)于Zend框架如何在不同模塊中使用相同的模型?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!