Zend framework 如何自动加载Zend Framework模块模型?

Zend framework 如何自动加载Zend Framework模块模型?,zend-framework,Zend Framework,我正在Zend框架中构建一个新的CMS,我对ZF没有太多接触。客户端需要两个名为Admin和FE的部分。因此,我按照如下方式构建了我的应用程序结构 - SITE -- application ---- configs ---- layouts ---- modules -------- default ------------ controllers ------------ forms ------------ models ------------ views ------------ Bo

我正在Zend框架中构建一个新的CMS,我对ZF没有太多接触。客户端需要两个名为Admin和FE的部分。因此,我按照如下方式构建了我的应用程序结构

- SITE -- application ---- configs ---- layouts ---- modules -------- default ------------ controllers ------------ forms ------------ models ------------ views ------------ Bootstrap.php -------- admin ------------ controllers ------------ forms ------------ models ------------ views ------------ Bootstrap.php ---- Bootstrap.php -- public -- library -- index.php 2.)我将如何为不同的模块使用不同的布局

这里有两个问题:

  • 自动加载模型
  • 模块特定布局
  • 对于自动加载模型,首先确保您的模块引导类扩展了Zend_应用程序_模块_引导。这将注册一个包含映射的资源自动加载程序,以便名为
    Admin\u model\u User
    的模型类可以存储在文件
    application/modules/Admin/models/User.php
    (注意路径名中的复数model*s*)。对于上面描述的用法,您似乎不需要自己定义任何此类映射

    默认模块有点棘手。IIRC,默认模块使用appnamespace,通常默认为
    Application
    。因此,例如,默认模块中的用户模型将命名为
    Application\u model\u user
    ,并存储在文件
    Application/modules/default/models/user.php
    中。[如果不起作用,请尝试命名
    Default\u Model\u User
    ]

    [然而,如果您真的坚持为您的管理模块使用一个空的appnamespace,为您的模型使用一个前缀CPModel(正如您的示例所示),那么其中的一些更改将发生。]

    结果是,由于这些文件夹中的大多数不在include_路径上,因此需要在某个时候告诉系统要将哪些类前缀与哪些目录关联/映射

    对于特定于模块的布局,通常我会创建一个实现
    preDispatch()
    hook的。如果您在
    application/layouts/scripts/
    中将布局保持在顶层,那么您的插件可以看起来像存储在
    application/plugins/Layout.php中的以下内容:

    class Application_Plugin_Layout extends Zend_Controller_Plugin_Abstract
    {
    
        public function preDispatch(Zend_Controller_Request_Abstract $request)
        {
            Zend_Layout::getMvcInstance()->setLayout($request->getModuleName());
        }
    }
    
    protected function _initPlugins()
    {
        $this->bootstrap('frontController');
        $front = $this->getResource('frontController');
        $front->registerPlugin(new Application_Plugin_Layout());
    }
    
    通过
    applications/config/application.ini
    ,在应用程序级
    Bootstrap
    中注册插件:

    resources.frontController.plugin.layout = "Application_Plugin_Layout"
    
    或者在
    application/Bootstrap.php
    中的应用程序级别
    Bootstrap

    class Application_Plugin_Layout extends Zend_Controller_Plugin_Abstract
    {
    
        public function preDispatch(Zend_Controller_Request_Abstract $request)
        {
            Zend_Layout::getMvcInstance()->setLayout($request->getModuleName());
        }
    }
    
    protected function _initPlugins()
    {
        $this->bootstrap('frontController');
        $front = $this->getResource('frontController');
        $front->registerPlugin(new Application_Plugin_Layout());
    }
    

    然后,例如,您的管理布局可以存储在
    application/layouts/scripts/admin.phtml

    Weinaub-您正在应用程序目录中创建“plugins.layout.php”文件,或者您正在“application/plugins”目录中创建“layout.php”文件???@Saurabh:感谢您捕捉到了输入错误固定的。在文件
    Application/plugins/Layout.php
    中名为
    Application\u Plugin\u Layout
    (注意路径中的多个插件)。我还认为在zf 1.11中不允许从Application.ini注册插件,我们必须将布局文件从“Layout.phtml”重命名为“default.phtml”才能工作。。请确认..@Saurabh您可以随意命名布局。您可以使用application.ini中的以下行设置默认布局:
    resources.layout.layout=master
    。是的,您可以在ZF 1.11中的application.ini中注册插件,它是。@Saurabh:您可以在
    application.ini
    v1.11中注册插件。唯一的限制是插件不需要任何构造函数参数。