Example _php instance of resource autoloading usages of Zend Framework tutorial

Source: Internet
Author: User
Tags zend zend framework

This example describes the resource autoloading usage in the Zend Framework. Share to everyone for your reference, specific as follows:

Typically, in a development application, a possible class file name cannot be defined in accordance with the recommendations of the standard Zend Framework, which means that your class files cannot be discovered by the auto loader. Zend_loader_autoloader_resource provides a solution.

A resource is simply a name that corresponds to a component's namespace (appended to the Autochanger's namespace) and the path (the basic path of the relative Autochanger), for example:

$loader = new Zend_application_module_autoloader Array (
  ' namespace ' => ' Blog ',
  ' BasePath ' => Application_path. '/modules/blog ',
));

Specific examples are 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

To create a resource loader:

$loader = new Zend_loader_autoloader_resource Array (
  ' basepath ' => ' path/to/some/resources/', '
  Namespace ' => ' Foo ',
));

Defining resource types

Zend_loader_autoloader_resourse::addresourcetype () has three parameters: the resource name, the relative resource path name of the specified resource path, and the resource type component prefix.

In the tree above, we have three resource types: form (in subdirectory forms, resource prefix is form), model (in subdirectory models, resource prefix is model), and dbtable (in 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 in the constructor

$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 ();

Automatic loading of resources in a module

MVC in the Zend framework encourages the use of "modules," which, by default, typically has some resource types, and the Zend framework provides a standard directory layout for a module. In this paradigm, the resource loader is very useful, and they are enabled by default.

The basic directory structure of the module:

<modulename>
configs/
Application.ini
controllers/
helpers/
forms/
layouts/
filters/
helpers/
scripts/
models/
services/
views/
filters/
helpers/
scripts/
bootstrap.php

You can extend Zend_application_module_bootstrap to create a module-boot class bootstrap.php, with specific resource loads similar to the default resource load usage.

More interested in Zend related content readers can view the site topics: "The introduction of the Zend Framework frame", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Course", "PHP object-oriented Programming Program , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

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.