How to autoload Zend Framework module models?

后端 未结 1 1729
清歌不尽
清歌不尽 2021-01-07 13:35

I am building a new CMS in Zend Framework and I don\'t have much exposure to ZF. Client requires two sections called Admin and FE. So, I have structured my application struc

相关标签:
1条回答
  • 2021-01-07 14:04

    Two questions here:

    1. Autoloading models
    2. Module-specific layout

    For autoloading models, first make sure that your module bootstrap class extends Zend_Application_Module_Bootstrap. This will register a resource autoloader that includes a mapping so that a model class named Admin_Model_User can be stored in the file application/modules/admin/models/User.php (note the plural model*s* in the path name). For the usage you describe above, it does not appear that you need to define any such mappings yourself.

    There is a bit of trickiness associated to the default module. IIRC, the default module uses the appnamespace, typically defaulting to Application_. So, for example, a user model in the default module would be named Application_Model_User and stored in the file application/modules/default/models/User.php. [If that doesn't work, then try naming Default_Model_User]

    [However, if you really insist on an empty appnamespace for your admin module and a prefix of CPModel for your models - as your example suggests - then some of this changes.]

    The upshot is that since most of these folders are not on the include_path, the system needs to be told at some point what class prefixes to associate/map with what directories.

    For module-specific layouts, typically I create a front-controller plugin that implements the preDispatch() hook. If you keep your layouts at the top-level in application/layouts/scripts/, then your plugin can look something like the following stored in 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());
        }
    }
    

    Register your plugin in your app-level Bootstrap, either via applications/config/application.ini:

    resources.frontController.plugin.layout = "Application_Plugin_Layout"
    

    or in the app-level Bootstrap in application/Bootstrap.php:

    protected function _initPlugins()
    {
        $this->bootstrap('frontController');
        $front = $this->getResource('frontController');
        $front->registerPlugin(new Application_Plugin_Layout());
    }
    

    Then, for example, your admin layout could be stored in application/layouts/scripts/admin.phtml.

    0 讨论(0)
提交回复
热议问题