Zend Framework modular application, cannot load models for each module, autoload models?

Is there a way to have models for each module? I have 3 modules, one is the contacts module. I created a model for it in modules / contacts / models / Codes.php Code controller

class Contacts_CodesController extends Zend_Controller_Action { public function init() { /* Initialize action controller here */ $this->view->messages = $this->_helper->flashMessenger->getMessages(); } public function indexAction() { $codesTable = new Contacts_Model_Codes(); } 

Code Model:

 class Contacts_Model_Codes extends Zend_Db_Table { protected $_name = 'codes'; } 

The error I get: Fatal error: Class 'Contacts_Model_Codes' not found in / Applications / MAMP / htdocs / zf _site / application / modules / contacts / controllers / CodesController.php on line 26

thanks

+6
module zend-framework model
source share
5 answers

I found a problem. I forgot to put the boot file in my contacts module. Now everything works, and my modules can use their own models.

 class Contacts_Bootstrap extends Zend_Application_Module_Bootstrap { } 
+18
source share

I found a solution, I think! :) This is a problem when adding the following resource to the application.ini file

 resources.frontController.defaultModule = "Default" 

and also you use some parameters. I think this is a mistake.

The correct way to implement modules is:

1 - Create the necessary modules and the default module using the zf tool

2 - In apllication.ini, tell ZF where the modules are and where the controllers of these modules are located, with

 resources.frontController.moduleDirectory = APPLICATION_PATH "/modules" resources.frontController.moduleControllerDirectoryName = "controllers" 

Use famous

 resources.modules = "" 

And install:

 resources.frontController.params.prefixDefaultModule = "" 

This is important because the zf tool sets the value to "1". Here is the error. :)

And remember, DO NOT START THAT THE DEFAULT MODULE!

3 - Create a boot file for each module and put:

If my module is "Evacol":

 <?php class Evacol_Bootstrap extends Zend_Application_Module_Bootstrap { } 

Save it in / modules / Evacol / obviously

Pay attention to Evacol _ ... and ..._ Module_Bootstr ... NAME OF MY MODULE EXTENDING THE RIGHT CLASS. Do not use the default value for the boot file created with the zf tool. I did it :)

CANNOT CHANGE ANYTHING. THIS IS NOT NECESSARY.

And voila! Trust me. He works!

It was Zend Framework 1.10.8

+6
source share

You need to register the Contacts namespace with the autoloader. You can use Zend_Application_Module_Autoloader for this.

 $autoloader = new Zend_Application_Module_Autoloader(array( 'namespace' => 'Contacts_', 'basePath' => dirname(__FILE__) . '/modules/cotacts', )); 

This will create the following mappings for your module inside the provided basePath.

 api/ => Api forms/ => Form models/ => Model DbTable/ => Model_DbTable plugins/ => Plugin 

If you use Zend_Application to extend your application and its modules, you do not need this because the docs say that:

When using modular bootstraps with Zend_Application, an instance of Zend_Application_Module_Autoloader will be created by default for each discrete module, which allows you to automatically load module resources.

+5
source share

add

 resources.modules[] = 

In your ini configuration

+5
source share

I am using version 1.9. This is part of my boot file:

 protected function _initAutoload() { $modelLoader = new Zend_Application_Module_Autoloader(array( 'namespace' => '', 'basePath' => APPLICATION_PATH.'/modules/default') ); } protected function _initAutoloaders() { $this->getApplication()->setAutoloaderNamespaces(array('Eric_')); return $this; } protected function _initPlugins() { $this->bootstrap('autoloaders'); $this->bootstrap('frontController'); // register the plugin for setting layouts per module $plugin = new Eric_Plugin_Modularlayout(); $this->frontController->registerPlugin($plugin); return $modelLoader; } 

The Eric_Plugin_Modularlayout plugin sets the correct layout for each module.

I have 3 modules: default, ez, contacts. The funny thing is: in the action of the contacts, I can call the models in the ez / models directory. no problem.

+2
source share

All Articles