Zend框架如何在不同模块中使用相同的模型?

我正在一个现有项目中实施 Zend Framework,该项目有一个公共营销区、一个私有成员区、一个管理站点和一个营销活动管理站点.目前,营销区域和成员区域的控制器脚本都在网站的根目录下,然后是单独的 admin 文件夹和营销活动网站的另一个文件夹.

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.

在实施 Zend 框架时,我希望能够将控制器和视图拆分为模块(一个用于成员区域,一个用于公共营销区域,一个用于管理站点,一个用于营销活动管理站点),但我需要能够将每个模块指向同一个模型,因为所有三个组件都在同一个数据库和同一个业务对象上工作.

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.

但是,我无法在文档中找到有关如何执行此操作的任何信息.任何人都可以提供有关如何执行此操作的链接或有关如何完成此操作的一些简单说明的帮助吗?

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?

推荐答案

我所做的是将通用类保存在模块层次结构之外的库"目录中.然后设置我的 INCLUDE_PATH 使用相应模块的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/
. . .

在我的引导脚本中,我将application/library/"添加到INCLUDE_PATH.然后在每个控制器的 init() 函数中,我将该模块的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() 等函数不会将各自的模型目录添加到 INCLUDE_PATH.无论如何,您必须自己执行此操作.以下是如何操作的一个示例:

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()
    )
  )
);

您可以在引导程序中将library"添加到您的路径中,但是您不能在引导程序中为正确的模块添加models"目录,因为模块依赖于路由.有些人在他们的控制器的 init() 方法中这样做,有些人为 ActionController 的 preDispatch 钩子编写了一个插件来设置 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.

相关文章