Введение в Zend Framework

 Learning Zend Framework


 Zend Framework Reference

  • Zend_Gdata
  • Zend_Http
  • Zend_InfoCard
  • Zend_Json
  • Zend_Layout
  • Zend_Ldap
  • Zend_Loader
  • Zend_Locale
  • Zend_Log
  • Zend_Mail
  • Zend_Markup
  • Zend_Measure
  • Zend_Memory
  • Zend_Mime
  • Zend_Navigation
  • Zend_Oauth
  • Zend_OpenId
  • Zend_Paginator
  • Zend_Pdf
  • Zend_ProgressBar
  • Zend_Queue
  • Zend_Reflection
  • Zend_Registry
  • Zend_Rest

  • Zend_Search_Lucene
  • Zend_Serializer
  • Zend_Server
  • Zend_Service
  • Zend_Session
  • Zend_Soap
  • Zend_Tag
  • Zend_Test
  • Zend_Text
  • Zend_TimeSync
  • Zend_Tool
  • Zend_Tool_Framework
  • Zend_Tool_Project
  • Zend_Translate
  • Zend_Uri
  • Zend_Validate
  • Zend_Version
  • Zend_View
  • Zend_Wildfire
  • Zend_XmlRpc
  • ZendX_Console_Process_Unix
  • ZendX_JQuery
  • Ttranslation 27.4% Update 2010-11-28 - Revision 23238 - Version ZF 1.11.x

    42.3. 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.

    42.3.1. 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 has no component namespace, giving "My_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:

    $resourceLoader = new Zend_Loader_Autoloader_Resource(array(
    '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.


    Alternately, you could pass these as an array to addResourceTypes(); the following is equivalent to the above:

    'acl' => array(
    'path'      => 'acls/',
    'namespace' => 'Acl',
    'form' => array(
    'path'      => 'forms/',
    'namespace' => 'Form',
    'model' => array(
    'path'      => 'models/',

    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:

    $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/',

    42.3.2. 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
    /      => Model
    / => Model_DbTable
    / => Model_Mapper
    /     => Plugin
    /    => Service
    helpers  => View_Helper
    => 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.

    42.3.3. Using Resource Autoloaders as Object Factories

    42.3.4. Resource Autoloader Reference

    digg delicious meneame google twitter technorati facebook