6
votes

Is there a way to have models for each module? I have 3 modules, one is a "contacts" module. I created a model for it in modules/contacts/models/Codes.php Codes 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();

    }

Codes 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

5

5 Answers

18
votes

I found the problem. I forgot to put a bootstrap file in with my contacts module. Now it all works and I can have my modules use their own models.

class Contacts_Bootstrap extends Zend_Application_Module_Bootstrap
{

}
6
votes

I've found the solution, I guess! :) It's a problem when you add the next Resource in the application.ini file

resources.frontController.defaultModule = "Default"

and also you use some kind of parameters. I think that is a Bug.

The correct way to implement Modules is:

1 - Create your desired modules and the 'Default' Module with zf tool

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

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

Use the known

resources.modules = ""

And set:

resources.frontController.params.prefixDefaultModule = ""

It's important because zf tool set it to "1". Here is the bug. :)

And remember DO NOT PUT WHAT THE DEFAULT MODULE IS!!

3 - Create the bootstrap file for each module and put:

If my module is 'Evacol':

<?php
class Evacol_Bootstrap extends Zend_Application_Module_Bootstrap
{
}

Save it to /modules/Evacol/ obviously

Take note of Evacol_... and ..._Module_Bootstr... THE NAME OF MY MODULE EXTENDING THE CORRECT CLASS. Don't use the default value of bootstrap file created with zf tool. I did it :)

DON'T MODIFY ANYTHING ELSE. IT IS NOT NECESARY.

And voila! Trust me. It works!

It was Zend Framework 1.10.8

5
votes

You have to register the 'Contacts_' namespace with the auto loader. 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 basePath you provide.

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

If you are using Zend_Application to boostrap your application and it' modules you should not need this because the docs say that:

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.

5
votes

add

resources.modules[] =

To your config ini

2
votes

I'm using version 1.9. This is part of my bootstrap:

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 plugin Eric_Plugin_Modularlayout sets the correct layout for each module.

I have 3 modules: default, ez, contacts. The funny thing is, In a contacts action I can call the models in the ez/models dir. without a problem.