0

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!

John Cartwright
  • 5,109
  • 22
  • 25
  • Sorry my description was wrong. I am not including forms, but form elements. This realization gave me the idea to check that Form_Element was actually mapped in Zend_Application_Module_Autoloader, which it turned out it was not. By adding this to the resource types, everything started working. – John Cartwright Mar 31 '11 at 20:10

1 Answers1

0

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.

Tim Fountain
  • 33,093
  • 5
  • 41
  • 69
  • See my comment for my self-realized answer. I've accepted this since it had some excellent suggestions, and may be useful for someone else with a similar issue. – John Cartwright Mar 31 '11 at 20:11