Search code examples
formszend-frameworkmodulezend-autoloader

Forms not found by autoloader in Zend module?


I'm getting pretty fed up with Zend's autoloader.

I'm trying to load EditPassword from within PasswordController in the module structure as shown below.

project structure

application.ini

includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.frontController.baseUrl = "/"
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"  
phpSettings.date.timezone = "Europe/London"

Bootstrap.php:

public function init() {
    $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV);
    $baseUrl = $config->baseHttp;
    define('BASE_URL', $baseUrl);
}

protected function _initAutoload() {
    $autoLoader = Zend_Loader_Autoloader::getInstance();
    $autoLoader->registerNamespace('App_');
    //
    $moduleLoader = new Zend_Application_Module_Autoloader(array(
            'namespace' => '',
            'basePath' => APPLICATION_PATH . 'modules/account',
            'resourceTypes' => array(
                    'form' => array(
                            'path' => 'forms',
                            'namespace' => 'Form'))));
    $autoLoader->pushAutoloader($moduleLoader);
    //
    return $autoLoader;
}

I'm loading the form in the controller like this:

$form = new Account_Form_EditPassword();
$this->view->form = $form;

The error itself is:

Fatal error: Class 'Account_Form_EditPassword' not found in 
Z:\dat\docs\workspace\metamusic\application\modules\account\controllers\PasswordController.php
on line 7

What am I doing wrong? Everything else in Zend seems to run off fairly sane defaults - do I really have to declare all the paths to forms/models/viewhelpers for each module, given that they're following the default Zend structure, and in the default modules directory?


Solution

  • I would remove all the module autoloading code from the app Bootstrap and then implement an empty module bootstrap in application/modules/account/Bootstrap.php:

    class Account_Bootstrap extends Zend_Application_Module_Bootstrap
    {
    }
    

    Then in application/configs/application.ini, add the following:

    resources.modules[] = 
    

    The idea here is that Zend_Application_Module_Bootstrap automatically registers a resource autoloader with bunch of common path/namespace mappings, including the one for forms that you are using.