Zend Framework: Autoloading module resources in config.ini?
Is it possible, to configure the following behaviour in application.ini?
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initAdminModuleAutoloader()
{
$this->_resourceLoader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Admin',
'basePath' => APPLICATION_PATH . '/modules/admin',
));
$this->_resourceLoader->addResourceTypes(array(
'model' => array(
'n开发者_JAVA百科amespace' => 'Model',
'path' => 'models'
)
));
}
}
?>
If so, can you please show us an example?
Thanks.
Here are a couple of pointers. First you should have the below line in your application.ini file.
resources.modules.admin = "enabled"
That will make sure that the following function in Zend_Application_Resource_Modules runs
public function init()
{
$bootstrap = $this->getBootstrap();
$bootstrap->bootstrap('FrontController');
$front = $bootstrap->getResource('FrontController');
$modules = $front->getControllerDirectory();
$default = $front->getDefaultModule();
$curBootstrapClass = get_class($bootstrap);
foreach ($modules as $module => $moduleDirectory) {
$bootstrapClass = $this->_formatModuleName($module) . '_Bootstrap';
if (!class_exists($bootstrapClass, false)) {
$bootstrapPath = dirname($moduleDirectory) . '/Bootstrap.php';
if (file_exists($bootstrapPath)) {
$eMsgTpl = 'Bootstrap file found for module "%s" but bootstrap class "%s" not found';
include_once $bootstrapPath;
if (($default != $module)
&& !class_exists($bootstrapClass, false)
) {
throw new Zend_Application_Resource_Exception(sprintf(
$eMsgTpl, $module, $bootstrapClass
));
} elseif ($default == $module) {
if (!class_exists($bootstrapClass, false)) {
$bootstrapClass = 'Bootstrap';
if (!class_exists($bootstrapClass, false)) {
throw new Zend_Application_Resource_Exception(sprintf(
$eMsgTpl, $module, $bootstrapClass
));
}
}
}
} else {
continue;
}
}
if ($bootstrapClass == $curBootstrapClass) {
// If the found bootstrap class matches the one calling this
// resource, don't re-execute.
continue;
}
$moduleBootstrap = new $bootstrapClass($bootstrap);
$moduleBootstrap->bootstrap();
$this->_bootstraps[$module] = $moduleBootstrap;
}
return $this->_bootstraps;
}
In the above function your module bootstrap is called. You need to have a module bootstrap file in /application/modules/admin called Bootstrap.php with the following code in it:
class Admin_Bootstrap extends Zend_Application_Module_Bootstrap
{
}
I'm going to skip a few steps but if you trace through the inheritance classes this will lead to the following function being called in Zend_Application_Module_Autoloader
public function initDefaultResourceTypes()
{
$basePath = $this->getBasePath();
$this->addResourceTypes(array(
'dbtable' => array(
'namespace' => 'Model_DbTable',
'path' => 'models/DbTable',
),
'mappers' => array(
'namespace' => 'Model_Mapper',
'path' => 'models/mappers',
),
'form' => array(
'namespace' => 'Form',
'path' => 'forms',
),
'model' => array(
'namespace' => 'Model',
'path' => 'models',
),
'plugin' => array(
'namespace' => 'Plugin',
'path' => 'plugins',
),
'service' => array(
'namespace' => 'Service',
'path' => 'services',
),
'viewhelper' => array(
'namespace' => 'View_Helper',
'path' => 'views/helpers',
),
'viewfilter' => array(
'namespace' => 'View_Filter',
'path' => 'views/filters',
),
));
$this->setDefaultResourceType('model');
}
All of the above resource types will be covered by default for every module that you follow this pattern for. Any others that you need will need to be customized in a bootstrap file.
I am using Zend Framework Version 1.10.4 with a default project configuration. I activate all Modules in my application.ini with the following line:
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
Its important that you have a Bootstrap.php in your module directory. Without that, my models wasn't loaded correctly. The URL in your Browser seems to be case sensitive. For example: http://example.com/Admin/controller/action
So, it should work...
精彩评论