Zend Framework Modules with common resources

允我心安 提交于 2019-12-22 01:22:28

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/5504938/zend-framework-modules-with-common-resources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!