Autoloading forms and models for each module in Zend?

How can someone autoload each 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; } } 

The main question . How can I modify the bootstrap so that it loads each form and model from the Foo module?

Additional question . Is it possible to have a global autoloader that loads in forms and models from each module? If so, how?

Edit (most common questions about the problem):

  • The standard Zend naming conventions are used for classes. For example, Bar_Model_Article , Bar_Model_Mapper_Article , Bar_Model_DbTable_Article , Bar_Form_Publish , ... (And are placed in their corresponding folder.)

  • This is not only one module that does not load its classes, but that’s all.

  • There are no problems with autoload classes using the Zend autoloader when using a simple non-modular application with several models, maps, dbtables and forms.

Fix

As @Tim Fountain said, the bootstraps module did not start, which means that when loading in Zend, the automatic loading did not occur. In the end, I found that 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" 

I agree the global bootstrap will no longer work; but it is much better than the bootstraps module does not work. If anyone knows how to still have a global bootstrap, feel free to leave a comment. Hope this can help others with a similar problem.

+4
source share
2 answers

The module bootstrap class automatically installs the module autoloader, so you can delete your _initAutoload () object, leaving only the empty class, and everything should work. See: http://framework.zend.com/manual/en/zend.loader.autoloader-resource.html#zend.loader.autoloader-resource.module

Change It looks like your boot modules are not starting. This is not an unusual problem, since the way it all fits in can be a bit confusing. The quickest way to test this is to simply add the init method to one of them with an echo and exit and see if it will ever be output.

The bootstraps of the modules are loaded and run by the 'modules' resource in the Zend Application. You need to somehow initiate this resource, because ZF will not look for a hunt for modular bootstraps 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 configure the resource from the main Bootstrap file.

+5
source

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

 appnamespace = "Foo" 

I personally leave empty.

0
source

Source: https://habr.com/ru/post/1347344/


All Articles