0

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?

  • Update your question with how you load the form in your controller – Adrian World Aug 12 '11 at 23:00
  • Have done. Pretty sure that's OK though, I'm using the command line tool to create form templates, and auto-complete fills in the correct name. –  Aug 12 '11 at 23:17
  • That looks good. Sorry, but then what is the exact error you get? – Adrian World Aug 12 '11 at 23:19
  • I've updated the question, but the error's not very descriptive, I'm afraid. –  Aug 12 '11 at 23:29
  • spelling of 'account' is misspelled in bootstraper its 'acount' one 'c' is missing . – Mr Coder Aug 13 '11 at 01:14
  • Thanks. Not the cause of the error apparently, so I've modified to question to clarify. –  Aug 13 '11 at 01:25
  • What David Weinraub said is correct and should solve your problem. The convention is to setup module autoloaders in the *module* bootstrap, and Zend_Application_Module_Bootstrap does this by default. So all you need to do is create a bootstrap class for your account module and then you can remove the $moduleLoader stuff from your main bootstrap. – Tim Fountain Aug 13 '11 at 12:15

3 Answers3

6

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.

David Weinraub
  • 14,144
  • 4
  • 42
  • 64
  • Do I have to declare that bootstrap somewhere? I've tried as you suggest, but the initSomething() functions within it are not getting called, as if Zend is unaware of its presence. –  Aug 13 '11 at 19:01
  • Aaah, you probably have to enable the modules in `application.ini`. Updating the answer. – David Weinraub Aug 13 '11 at 19:37
  • 1
    Hey make sure you have both of the following lines `resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"` and `resources.modules[] = ` in your application.ini, I had to add the frontController line to get this to work. – Danuofr Apr 10 '13 at 21:17
  • Very helpful! I'm amazed that zendtool doesn't automatically create a bootstrap file. I was scratching my head for hours as to why I could load controllers/views but not models/forms etc. – Richard Nov 13 '13 at 07:24
0

In Bootstrap.php

'basePath' => APPLICATION_PATH . 'modules/acount'

Should be:

'basePath' => APPLICATION_PATH . 'modules/account'

Everything else looks ok

adlawson
  • 6,303
  • 1
  • 35
  • 46
0

If this a copy/paste from your actual config then you have a typo in 'modules/acount'

'basePath' => APPLICATION_PATH . 'modules/acount',

Depending on your ZF version you don't even need the autoloader in the bootstrap. You can remove it. The following in your ini does the same.

resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
Adrian World
  • 3,118
  • 2
  • 16
  • 25
  • Thanks for the continued input. I've fixed that, but to no avail. I'm using the latest stable (1.11), so presumably that line in the ini was counteracting my typo. –  Aug 13 '11 at 00:53
  • @danH I'd say it's the other way around your initAutoload() is counteraction the iniconfig. Have you removed the autoloader in the bootstrap? – Adrian World Aug 13 '11 at 00:58
  • Yep, no luck. Removed absolutely everything else in the bootloader just to be sure, but no change. Thank you for your help by the way, it's much appreciated. –  Aug 13 '11 at 01:10