Autoloading modules

View: New views
5 Messages — Rating Filter:   Alert me  

Autoloading modules

by Jurian Sluiman :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
Hi all,
I have read some autoloading articles here, but all examples are with no or
one module. My application has this structure (almost everything is inside a
module):


/application
/configs
/layouts
/modules
/default
/admin
/blog
/calendar


I have read this thread: http://www.nabble.com/Autoloading-module-resources-
with-1.8-Preview-td22956945.html. It does not end with a conclusion and the
documentation is not clear about the Zend_Application_Module_Loader.


How can I autoload all my modules? I can create for each module an autoloader
by scanning the application/modules directory. The thread mentioned above has
another approach: each module contains its own bootstrap with config. Are
there any advices about this rather new subject (the manual seems even unfinished)?


Best regards,
Jurian



Re: Autoloading modules

by weierophinney :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

-- Jurian Sluiman <subscribe@...> wrote
(on Monday, 04 May 2009, 02:20 PM +0200):

> I have read some autoloading articles here, but all examples are with no or
> one module. My application has this structure (almost everything is inside a
> module):
>
>
> /application
> /configs
> /layouts
> /modules
> /default
> /admin
> /blog
> /calendar
>
>
> I have read this thread: http://www.nabble.com/Autoloading-module-resources-
> with-1.8-Preview-td22956945.html. It does not end with a conclusion and the
> documentation is not clear about the Zend_Application_Module_Loader.
>
> How can I autoload all my modules? I can create for each module an autoloader
> by scanning the application/modules directory. The thread mentioned above has
> another approach: each module contains its own bootstrap with config. Are
> there any advices about this rather new subject (the manual seems even
> unfinished)?

In each module, create a bootstrap that extends
Zend_Application_Module_Bootstrap, with the class name
<Modulename>_Bootstrap. That will setup an autoloader using
Zend_Application_Module_Autoloader for that module which follows the
recommended directory structure guidelines.

If you need additional configuration per-module, then you're all setup
to do so; otherwise, that's all you need.

--
Matthew Weier O'Phinney
Project Lead            | matthew@...
Zend Framework          | http://framework.zend.com/

Re: Autoloading modules

by Jurian Sluiman :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
Op Monday 04 May 2009 15:05:00 schreef Matthew Weier O'Phinney:
> -- Jurian Sluiman <subscribe@...> wrote
>
> (on Monday, 04 May 2009, 02:20 PM +0200):
> > I have read some autoloading articles here, but all examples are with no
> > or one module. My application has this structure (almost everything is
> > inside a module):
> >
> >
> > /application
> > /configs
> > /layouts
> > /modules
> > /default
> > /admin
> > /blog
> > /calendar
> >
> >
> > I have read this thread:
> > http://www.nabble.com/Autoloading-module-resources-
> > with-1.8-Preview-td22956945.html. It does not end with a conclusion and
> > the documentation is not clear about the Zend_Application_Module_Loader.
> >
> > How can I autoload all my modules? I can create for each module an
> > autoloader by scanning the application/modules directory. The thread
> > mentioned above has another approach: each module contains its own
> > bootstrap with config. Are there any advices about this rather new
> > subject (the manual seems even unfinished)?
>
> In each module, create a bootstrap that extends
> Zend_Application_Module_Bootstrap, with the class name
> <Modulename>_Bootstrap. That will setup an autoloader using
> Zend_Application_Module_Autoloader for that module which follows the
> recommended directory structure guidelines.
>
> If you need additional configuration per-module, then you're all setup
> to do so; otherwise, that's all you need.


Thanks for your quick answer. I added in each module directory a Bootstrap.php. E.g. in application/modules/blog/Bootstrap.php i have a Blog_Bootstrap extending Zend_Application_Module_Bootstrap.
In the global bootstrap I have added the application/module directory as a module directory (I had that already).


Still the AclController (located at application/modules/default/controllers/AclController.php) isn't found. Some controllers are required in the _initFrontController() method of the global bootstrap class. Do I miss something?


Regards, Jurian


Re: Autoloading modules

by dmitrybelyakov :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Jurian Sluiman wrote:
Thanks for your quick answer. I added in each module directory a
Bootstrap.php. E.g. in application/modules/blog/Bootstrap.php i have a
Blog_Bootstrap extending Zend_Application_Module_Bootstrap.
In the global bootstrap I have added the application/module directory as a
module directory (I had that already).

Still the AclController (located at
application/modules/default/controllers/AclController.php) isn't found. Some
controllers are required in the _initFrontController() method of the global
bootstrap class. Do I miss something?

Regards, Jurian

Same problem for me here. It seems that autoloading works nice for all modules, except the default one.

So the same code (in frontcontroller plugin) works fine

    $initProcedure = new Modulename_Model_Initialize();

unless i make 'Modulename' my default module in application config. I wonder what's wrong with that (or nothing maybe :) but it's quite unclear what is actually happening.


Dmitry.





Re: Autoloading modules

by dmitrybelyakov :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

There is some more on the topic i discovered after tweaking it around: the problem seems to be because of default module actually being 'default' that makes Zend MVC treat it like an ordinary application folder and NOT like a module folder.

That's the reason why module's Bootstrap is ignored and global application Bootstrap is used. In other words your global application bootstrap must be under your default module folder. It seems a little strange to me, as i would prefer all my modules be treated the same way and have application Bootstrap hold al common things for all the application and module Bootstraps handle module-dependent configurations.

I found some topics here on the list addressing similar problem but no practical solution still.

I found two ways of handling such cases but they both just don't seem right to me and appear to be a little hacky.

So the first one is to create special _initAutoloader() method in global Bootstrap specially for the default module (with module name as a namspace):

    protected function _initAutoload()
    {

        $autoloader = new Zend_Application_Module_Autoloader(array(
            'namespace' => 'Modulename',
            'basePath'  =>  APPLICATION_PATH . '/modules/modulename'
        ));
        return $autoloader;
    }
   
I don't like the idea of treating my default module in 'special' way, i would rather prefer it treated regularly like all the other modules. But still it allows autoloading default module's resources.


The other approach is to tweak application config and point my controlerDirectory to default module's controllers directory and do not specify default module at all.

# controllers
resources.frontController.controllerDirectory = APPLICATION_PATH "/modules/modulename/controllers"
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
#resources.frontController.defaultModule = "modulename"   <-- remove this

This allows to use both global application Bootstrap and default Module's bootstrap BUT seems to be a very much a hack.

So there's a lot of confusion here fore me and i would appreciate if someone could make this more clear.


Thank you.
Dmitry.