- path/to/some/directory/
- acls/
- Site.php
- forms/
- Login.php
- models/
- User.php
Programmer's Reference Guide
| The Autoloader |
Resource Autoloaders
Resource autoloaders are intended to manage namespaced library code that follow Zend Framework coding standard guidelines, but which do not have a 1:1 mapping between the class name and the directory structure. Their primary purpose is to facilitate autoloading application resource code, such as application-specific models, forms, and ACLs.
Resource autoloaders register with the autoloader on instantiation, with the namespace to which they are associated. This allows you to easily namespace code in specific directories, and still reap the benefits of autoloading.
Resource autoloader usage
Let's consider the following directory structure:
Within this directory, all code is prefixed with the namespace "My_". Within the "acls" subdirectory, the component prefix "Acl_" is added, giving a final class name of "My_Acl_Site". Similarly, the "forms" subdirectory maps to "Form_", giving "My_Form_Login". The "models" subdirectory maps to "Model_", giving "My_Model_User".
You can use a resource autoloader to autoload these classes. To instantiate the resource autoloader, you are required to pass at the minimum the base path and namespace for the resources it will be responsible for:
- 'basePath' => 'path/to/some/directory',
- 'namespace' => 'My',
- ));
注意: Base namespace
In Zend_Loader_Autoloader, you are expected to provide the trailing underscore ("_") in your namespace if your autoloader will use it to match the namespace. Zend_Loader_Autoloader_Resource makes the assumption that all code you are autoloading will use an underscore separator between namespaces, components, and classes. As a result, you do not need to use the trailing underscore when registering a resource autoloader.
Now that we have setup the base resource autoloader, we can add some components to it to autoload. This is done using the addResourceType() method, which accepts three arguments: a resource "type", used internally as a reference name; the subdirectory path underneath the base path in which these resources live; and the component namespace to append to the base namespace. As an example, let's add each of our resource types.
- $resourceLoader->addResourceType('acl', 'acls/', 'Acl')
- ->addResourceType('form', 'forms/', 'Form')
- ->addResourceType('model', 'models/', 'Model');
Alternately, you could pass these as an array to addResourceTypes(); the following is equivalent to the above:
Finally, you can specify all of this when instantiating the object, by simply specifying a "resourceTypes" key in the options passed and a structure like that above:
- 'basePath' => 'path/to/some/directory',
- 'namespace' => 'My',
- 'path' => 'acls/',
- 'namespace' => 'Acl',
- ),
- 'path' => 'forms/',
- 'namespace' => 'Form',
- ),
- 'path' => 'models/',
- 'namespace' => 'Model',
- ),
- ),
- ));
The Module Resource Autoloader
Zend Framework ships with a concrete implementation of Zend_Loader_Autoloader_Resource that contains resource type mappings that cover the default recommended directory structure for Zend Framework MVC applications. This loader, Zend_Application_Module_Autoloader, comes with the following mappings:
- forms/ => Form
- models/ => Model
- DbTable/ => Model_DbTable
- mappers/ => Model_Mapper
- plugins/ => Plugin
- services/ => Service
- views/
- helpers => View_Helper
- filters => View_Filter
As an example, if you have a module with the prefix of "Blog_", and attempted to instantiate the class "Blog_Form_Entry", it would look in the resource directory's "forms/" subdirectory for a file named "Entry.php".
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.
Using Resource Autoloaders as Object Factories
Resource Autoloader Reference
| The Autoloader |
Add A Comment
Please do not report issues via comments; use the ZF Issue Tracker.
If you have a JIRA/Crowd account, we suggest you login first before commenting.

Comments
I add a resource to the resource autoloader like in the examples above. for my example I will use form.
$resourceLoader->addResourceType('form', 'forms/', 'Form');
On my local machine my directory can look like the following example, and can auto load the files without error given that the class name follows the directory.
forms/
- Login.php (Form_Login)
- Signup.php (Form_Signup)
- Admin/
- - News.php (Form_Admin_News)
- - User.php (Form_Admin_User)
But on my remote test server the auto loader fails and returns an error:
Fatal error: Class 'Form_Admin_News' not found in /application/controllers/AdminController.php on line 53
on my Dev System (ZendserverCE on MAC) al walks fine, but on my production system with ubuntu 8.04 it fail:
Fatal error: Class 'Media_Model_DbTable_Sources' not found in /www/projectname/application/modules/media/controllers/SourcesController.php on line 20
I can not find a solution. :(
d
I'm using this:
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => 'path/to/some/directory',
'namespace' => 'My',
'resourceTypes' => array(
'acl' => array(
'path' => 'acls/',
'namespace' => 'Acl',
),
'form' => array(
'path' => 'forms/',
'namespace' => 'Form',
),
'model' => array(
'path' => 'models/',
),
),
));
It's copied from this tutorial.
I'm getting an Exception:
Fatal error: Uncaught exception 'Zend_Loader_Exception' with message 'Initial definition of a resource type must include a namespace' in C:\Program Files\Zend\ZendServer\share\ZendFramework\library\Zend\Loader\Autoloader\Resource.php:276 Stack trace: #0
...
Why?!
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => 'path/to/some/directory',
'namespace' => 'My',
'resourceTypes' => array(
'acl' => array(
'path' => 'acls/',
'namespace' => 'Acl',
),
'form' => array(
'path' => 'forms/',
'namespace' => 'Form',
),
'model' => array(
'path' => 'models/',
),
),
));
Note the missing namespace in the 'model' resource type. That example should actually be:
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => 'path/to/some/directory',
'namespace' => 'My',
'resourceTypes' => array(
'acl' => array(
'path' => 'acls/',
'namespace' => 'Acl',
),
'form' => array(
'path' => 'forms/',
'namespace' => 'Form',
),
'model' => array(
'path' => 'models/',
'namespace' => 'Model',
),
),
));
Even if you set things up as shown in the above example don't forget you need to include the namespaces names in your class names too!
So if you have:
$user = new My_Model_User();
and this refers to the User.php file sat under application_directory/models/User.php
you still must make sure that the user class name is actually My_Model_User
class My_Model_User {
//code
}
Zend do this themselves too. For example if you load the config.php class in the Zend library directory via the autoloader you will see that the class name in that file is Zend_Config.php
If you would name the class:
class User {
}
then you will get the "class not found" error.
This may seem obvious to many but it had me baffled when I started out using the Zend Framework in the MVC style.
I think Zend could give more detailed examples as that would help.
Alguien me explica?
Indeed, I was struggling to solve the 'Class Model_Users' not found error to finally realize that the mistake was because the file was wrongly named 'Model_Users.php' and not 'Users.php' as expected.
Cheers!
Any idea how to resolve this?
path/to/some/directory/
acls/
Site.php (ENSURE YOUR class's filename starts with UPPER CASE)
forms/
Login.php (ENSURE YOUR class's filename starts with UPPER CASE)
models/
User.php (ENSURE YOUR class's filename starts with UPPER CASE)
Zend_Loader_Autoloader works Capitalized!!
My_Acl_Site points to -->
/acls/Site.php NOT /acls/site.php
I'm new to Zend Framework, and i'm trying to understand something, what are the benefits of using the Zend_Application_Module_Autoloader and the namespace "My_" when im using the modules structure. Why not simply call the controller and classes by its names. What am I trying to avoid when using those tools?