Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework modular app, can't load models for each module, autoloading models?

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

like image 674
EricP Avatar asked Oct 06 '09 03:10

EricP


3 Answers

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
{

}
like image 167
EricP Avatar answered Oct 13 '22 15:10

EricP


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

like image 38
RowG Avatar answered Oct 13 '22 14:10

RowG


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.

like image 36
Goran Jurić Avatar answered Oct 13 '22 13:10

Goran Jurić