Zend Framework 1.8+ Module/Library Autoloading

Given the problems I had for the 1st Zend Framework website I did, I figured the next time I started one from scratch I would document how to get the autoloading working, since almost nowhere has it documented correctly. I forgot then too, so 3rd time lucky…

The following code assumes the following:

  • You have no model, controller or view directories at the root of application/ because they should all be within application/modules/modulename/
  • The default module is located at application/modules/default/
  • The default module is not namespaced, so the index controller is called IndexController, not Default_IndexController
You can change all of the above in application.ini and/or Bootstrap.php if so desired.

The key things you need to add, excluding the directory structure that will be created for you by Zend Tool is the following entry in application.ini in application/configs/

resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"

Then put the following code in the Application (not any of the Modules) Bootstrap.php in application/

protected function _initAutoloader()
{

	$moduleAutoloader =
		new Zend_Application_Module_Autoloader(array(
			'namespace' => '',
			'basePath' => APPLICATION_PATH.'/modules/default'
		));

}

Why? No idea, as according to Zend you can do it all from setting the Module Resource in application.ini, although I have never gotten that to work with a variety of different configuration options…

If you need to register a namespace for the library you can add this to Bootstrap.php also

$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('MyNamespace_');

Add comment