How to Use ResourceAutoloading in ZendFramework tutorial _ php instance

Source: Internet
Author: User
This article mainly introduces the use of ResourceAutoloading in the ZendFramework tutorial, and analyzes the principle and usage of ResourceAutoloading for automatic loading based on the instance form, for more information about Resource Autoloading in Zend Framework, see the following example. We will share this with you for your reference. The details are as follows:

Generally, in development applications, the class file name may not be defined as recommended by the standard Zend Framework, which means that your class file cannot be detected by the automatic loader. Zend_Loader_Autoloader_Resource provides a solution.

A resource is only a namespace (The namespace appended to the auto-loader) and path (the basic path of the relative auto-loader) corresponding to a component. For example, you can:

$loader = new Zend_Application_Module_Autoloader(array(  'namespace' => 'Blog',  'basePath' => APPLICATION_PATH . '/modules/blog',));

The specific example is as follows:

Path/to/some/resources/
| -- Forms/
| '-- Guestbook. php // Foo_Form_Guestbook
| -- Models/
| -- DbTable/
| '-- Guestbook. php // Foo_Model_DbTable_Guestbook
| -- Guestbook. php // Foo_Model_Guestbook
| '-- GuestbookMapper. php // Foo_Model_GuestbookMapper

Create a resource Loader:

$loader = new Zend_Loader_Autoloader_Resource(array(  'basePath' => 'path/to/some/resources/',  'namespace' => 'Foo',));

Define resource types

Zend_Loader_Autoloader_Resourse: addResourceType () has three parameters: Resource Name, relative resource path of the specified resource path, and resource type component prefix.

In the preceding tree, we have three resource types: form (in the subdirectory forms, the resource prefix is Form), model (in the subdirectory models, the resource prefix is Model ), and dbtable (in the subdirectory "models/DbTable", the resource prefix is "Model_DbTable ").

The specific definition is as follows:

$loader->addResourceType('form', 'forms', 'Form')    ->addResourceType('model', 'models', 'Model')    ->addResourceType('dbtable', 'models/DbTable', 'Model_DbTable');

You can also specify

$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',    ),  ),));

Use to define Resources

$form   = new Foo_Form_Guestbook();$guestbook = new Foo_Model_Guestbook();

Automatically load Resources in the module

The Zend Framework MVC encourages the use of "modules". By default, modules usually have some resource types. Zend Framework provides a standard directory layout for a module. In this paradigm, resource auto-loaders are very useful and they are enabled by default.

Basic directory structure of the module:


Configs/
Application. ini
Controllers/
Helpers/
Forms/
Layouts/
Filters/
Helpers/
Scripts/
Models/
Services/
Views/
Filters/
Helpers/
Scripts/
Bootstrap. php

You can expand Zend_Application_Module_Bootstrap to create a module Bootstrap. php class. The specific resource loading is similar to the default resource loading.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.