Zend Framework Modules with common resources
I'm having a lot of trouble figuring out how we can have a modular directory structure, with the ability to load resources that are to be shared across modules. I.e.,
application
--- /forms
--- /models
--- /modules
---开发者_运维技巧---/module1/
---------/models
------/module2/
---------/models
Now, what I'm trying to do is load forms in /application/forms from within the modules. Everything I've tried results in these classes to not being loaded.
I've tried: 1) Letting Zend try and figure it out automagically. 2) Specifying all the paths in the main bootstrap for the application path as well as the modules. I.e.,
protected function _initAutoload()
{
$front = $this->bootstrap("frontController")->frontController;
$modules = $front->getControllerDirectory();
$default = $front->getDefaultModule();
$moduleloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Application',
'basePath' => APPLICATION_PATH
));
foreach (array_keys($modules) as $module) {
$moduleloader = new Zend_Application_Module_Autoloader(array(
'namespace' => ucfirst(strtolower($module)),
'basePath' => $front->getModuleDirectory($module))
);
}
}
3) Smashing my head on my desk many times.
.. and yes, I realize I do not need that loop for modules, as I have blank bootstraps in each module directory.
Any suggestions are welcome. Thanks!
Try this:
protected function _initAutoload()
{
$autoloader = new Zend_Application_Autoloader_Resource(array(
'namespace' => 'Application',
'basePath' => APPLICATION_PATH,
'resourceTypes' => array(
'form' => array(
'path' => 'forms/',
'namespace' => 'Form'
)
)
));
return $autoloader;
}
you don't need the module part, as you already seem to know. Add other resource types as required.
Since this is very close to what you have already, there may be another issue. The above should work assuming that:
- APPLICATION_PATH points at the /application directory in your app
- The form classes are named Application_Form_Something
- The filenames of these classes are Something.php (case sensitive)
e.g. if you have a contact form, you might call the class Application_Form_Contact
and this would live at application/forms/Contact.php
.
If you're still having issues, please include an example of a form class that isn't being found, along with how and where you are calling it from.
精彩评论