- |-- forms/
- | `-- Guestbook.php // Foo_Form_Guestbook
- |-- models/
- | |-- DbTable/
- | | `-- Guestbook.php // Foo_Model_DbTable_Guestbook
- | |-- Guestbook.php // Foo_Model_Guestbook
- | `-- GuestbookMapper.php // Foo_Model_GuestbookMapper
Resource Autoloading - Autoloading in Zend Framework
Often, when developing an application, it's either difficult to package classes in the 1:1 classname:filename standard Zend Framework recommends, or it's advantageous for purposes of packaging not to do so. However, this means you class files will not be found by the autoloader.
If you read through the design goals for the autoloader, the last point in that section indicated that the solution should cover this situation. Zend Framework does so with Zend_Loader_Autoloader_Resource.
A resource is just a name that corresponds to a component namespace (which is appended to the autoloader's namespace) and a path (which is relative to the autoloader's base path). In action, you'd do something like this:
- $loader = new Zend_Application_Module_Autoloader(array(
- 'namespace' => 'Blog',
- 'basePath' => APPLICATION_PATH . '/modules/blog',
Once you have the loader in place, you then need to inform it of the various resource types it's aware of. These resource types are simply pairs of subtree and prefix.
As an example, consider the following tree:
Our first step is creating the resource loader:
- $loader = new Zend_Loader_Autoloader_Resource(array(
- 'basePath' => 'path/to/some/resources/',
- 'namespace' => 'Foo',
Next, we need to define some resource types. Zend_Loader_Autoloader_Resourse::addResourceType() has three arguments: the "type" of resource (an arbitrary string), the path under the base path in which the resource type may be found, and the component prefix to use for the resource type. In the above tree, we have three resource types: form (in the subdirectory "forms", with a component prefix of "Form"), model (in the subdirectory "models", with a component prefix of "Model"), and dbtable (in the subdirectory "models/DbTable", with a component prefix of "Model_DbTable"). We'd define them as follows:
- $loader->addResourceType('form', 'forms', 'Form')
- ->addResourceType('model', 'models', 'Model')
- ->addResourceType('dbtable', 'models/DbTable', 'Model_DbTable');
Once defined, we can simply use these classes:
- $form = new Foo_Form_Guestbook();
- $guestbook = new Foo_Model_Guestbook();
Note: Module Resource Autoloading
Zend Framework's MVC layer encourages the use of "modules", which are self-contained applications within your site. Modules typically have a number of resource types by default, and Zend Framework even recommends a standard directory layout for modules. Resource autoloaders are therefore quite useful in this paradigm -- so useful that they are enabled by default when you create a bootstrap class for your module that extends Zend_Application_Module_Bootstrap. For more information, read the Zend_Loader_Autoloader_Module documentation.