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.