views:

40

answers:

2

Hi Stackers,

What is the best way to access my models and forms from a controller of a module?

Let's explain with "pictures":

/application/module/storage/controllers/IndexController.php

needs to call readAction in the class called storage_Model_Files in

/application/module/storage/models/Files.php

I've made this app's dir structure and these forms and models with zf.sh (Zend_Tool).

I've read about all sorts of ways of manually including these files. I want to lazy load them much like everything is done automatically with the default module. I can't seem to find how in the docs.

Does that make sense?

I have:

resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"

in my application.ini file. So I can access my controllers fine.

Thanks for your help!

A: 

Use autoloading: http://zendframework.com/manual/en/zend.loader.autoloader-resource.html

Vladimir
"Resource autoloaders are intended to manage namespaced library code that follow Zend Framework coding standard guidelines, but which do not have a 1:1 mapping between the class name and the directory structure."However, I do have a 1:1 class mapping between the class name and the directory structure.I will amend my question to make this clearer.
sims
+1  A: 

You have to add the module explicitly. Your options are:

1.) add this to your application.ini

resources.modules.storage = "" 

and create a file /application/module/storage/Bootstrap.php

class Storage_Bootstrap extends Zend_Application_Module_Bootstrap
{
}

2.) Create a method in your Boostrap.php

protected function _initAutoload()
{
    $loader = Zend_Loader_Autoloader::getInstance();
    $autoloader = new Zend_Application_Module_Autoloader(array(
        'namespace' => 'Storage_',
        'basePath'  => APPLICATION_PATH . '/storage',
    ));

    return $autoloader;
}
Goran Jurić
Interesting. So, I only need to do this once per module? Will this also autoload forms?What does the application.ini entry: resources.modules.storage = "" do?
sims
Take a look at the link Vladimir provided. There is a section there about the Module Resource Autoloader. "When using module bootstraps with Zend_Application, an instance of Zend_Application_Module_Autoloader will be created by default for each discrete module, allowing you to autoload module resources."
Goran Jurić
BTW; you have to use either option 1 or 2. There is no need for both.
Goran Jurić
Perfect! OK, so, the two things I was doing was:1. class case - my module dir is all lower case so Storage_Bootstrap doesn't work but storage_Bootstrap does.2. I had done this before but, my module bootstrap class was extending Zend_Application_Bootstrap_Bootstrap not Zend_Application_Module_Bootstrap. That documentation link says it should just work - so I expect it to. It doesn't tell you what you need to glue it together. So thanks to Goran, it's all clear now and the glue is place. Thanks!
sims