开发者

Autoloading forms and models for each module in Zend?

How can someone autoload every form and model for each module? Consider the following file structure:

application/
    modules/
        foo/
            forms/
                Register.php
            models/
                Account.php
        Bootstrap.php
        bar/
            forms/
                Publish.php
            models/
                Article.php
        Bootstrap.php
    Bootstrap.php

And for example, in foo/Bootstrap.php you have the following (non-functional) code:

class Foo_Bootstrap extends Zend_Application_Module_Bootstrap
{
    protected function _initAutoLoad()
    {
        $loader = new Zend_Loader_Autoloader_Resource(array(
            'basePath'  => APPLICATION_PATH . '/modules/foo',
            'namespace' => 'Foo',
        ));

        $loader->addResourceType('form', 'forms', 'Form')
            ->addResourceType('model', 'models', 'Model');
        return $loader;
    }
}

Basic question: How can the bootstrap开发者_如何学Python be modified so that it does load every form and model from the Foo module?

Extra question: Is it possible to have a global autoloader that loads in forms and models from every module? If so, how?

Edit (most common questions about the issue):

  • The default Zend naming conventions are being used for classes. Such as Bar_Model_Article, Bar_Model_Mapper_Article, Bar_Model_DbTable_Article, Bar_Form_Publish, ... (And are being placed in their respective folder.)

  • It isn't just one module that doesn't get its classes loaded, it's all of them.

  • There is no problem autoloading classes using the Zend autoloader when using a plain no-module application with multiple models, mappers, dbtables and forms.

Fix

As @Tim Fountain mentioned the module bootstraps weren't being run, meaning none of the automatic loading occurred that's baked into Zend. Eventually, I found where the problem was in my case. I had to remove the following lines from my configuration:

bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"

Agreed, the global bootstrap won't work anymore; but it's a lot better than having module bootstraps not functioning. If anyone knows how to still have the global bootstrap, feel free to leave a comment. Hope this can be of help to others with a similar problem.


The module bootstrap class sets up the module autoloader automatically, so you can remove your example _initAutoload() function leaving just an empty class and it should all just work. See: http://framework.zend.com/manual/en/zend.loader.autoloader-resource.html#zend.loader.autoloader-resource.module

Edit: It sounds like your module bootstraps aren't being run. This is not an uncommon problem as the way it all fits together can be a bit confusing. The quickest way to verify this would be to just add an init method to one of them with an echo and an exit and see if it ever gets output.

Module bootstraps are pulled in and run by the 'modules' resource within Zend Application. You need to trigger this resource in some way because ZF won't go hunting around for module bootstraps just in case they are there. The most common way to do this is to include this line in your application.ini:

resources.modules[] = ""

alternatively you can manually setup the resource from your main Bootstrap file.


I also had it always like that. But since the release of 1.10 (wild guess), you can remove that bootstrap code and just put the following line in your application.ini:

appnamespace = "Foo"

I personally leave mine empty.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜