我所做的是将公共类保存在模块层次结构之外的“库”目录中。然后设置我INCLUDE_PATH
使用相应模块的“models”目录,加上公共的“library”目录。
docroot/
index.php
application/
library/ <-- common classes go here
default/
controllers/
models/
views/
members/
controllers/
models/
views/
admin/
controllers/
models/
views/
. . .
在我的引导脚本中,我会application/library/
在INCLUDE_PATH
. 然后在每个控制器的init()
函数中,我将该模块的“ models/
”目录添加到INCLUDE_PATH
.
编辑: 函数喜欢setControllerDirectory()
并且setModuleDirectory()
不将各自的模型目录添加到INCLUDE_PATH
. 在任何情况下,您都必须自己执行此操作。这是如何做到这一点的一个例子:
$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()
)
)
);
您可以在引导程序中将“ library
”添加到您的路径中,但您不能在models
引导程序中为正确的模块添加“”目录,因为该模块依赖于路由。有些人在init()
他们的控制器的方法中这样做,有些人为 ActionController 的 preDispatch 钩子编写一个插件来设置INCLUDE_PATH
.