View Single Post
  #7 (permalink)  
Old 04-16-2008, 01:43 PM
Elemental's Avatar
Elemental Elemental is offline
Senior Member
 
Join Date: Jul 2007
Posts: 119
Default

The plugin:
PHP Code:
<?php
/**
 * ModelLoader.php
 * (library/MyApp/Controller/Plugin/ModelLoader.php)
 */ 

require_once('Zend/Controller/Plugin/Abstract.php');

class 
MyApp_Controller_Plugin_ModelLoader extends Zend_Controller_Plugin_Abstract
{
    public function 
preDispatch(Zend_Controller_Request_Abstract $request)
    {
        
$moduleName $request->getModuleName();
        
$rootDir Zend_Registry::get('rootDir');
        
set_include_path(get_include_path() .
                         
PATH_SEPARATOR $rootDir '/application/modules/' $moduleName '/models/');
    }
}
in your bootstrap:
PHP Code:
...
$rootDir dirname(dirname(__FILE__));
...
Zend_Registry::set('rootDir',$rootDir);
...
$frontController Zend_Controller_Front::getInstance();
...
$frontController->registerPlugin(new MyApp_Controller_Plugin_ModelLoader());
...
$frontController->dispatch(); 
This will allow any models in your <module>/models dir to be loaded with autoload by adding the modules model path to the include path based on the requested module. This will not allow models from non-requested modules to be loaded. I have a "global" model space in application/models that holds all models needed by the core system or more than one module.
Reply With Quote