Home php教程 PHP开发 Resource Autoloading usage example of Zend Framework tutorial

Resource Autoloading usage example of Zend Framework tutorial

Jan 03, 2017 pm 12:57 PM
zend framework

The example in this article describes the usage of Resource Autoloading in Zend Framework. Share it with everyone for your reference, the details are as follows:

Usually, when developing applications, the class file name may not be defined according to the standard Zend Framework recommendations, which means that your class file cannot be automatically loaded by the loader Discover. Zend_Loader_Autoloader_Resource provides the solution.

A resource is just a name corresponding to a component's namespace (namespace appended to the autoloader) and path (relative to the base path of the autoloader). For example, it can be like this:

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

The 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

Create resource loader:

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

Define resource type

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

In the above 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, the resource prefix is ​​Model) In the directory "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');
Copy after login

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',
    ),
  ),
));
Copy after login

in the constructor

Use to define resources

$form   = new Foo_Form_Guestbook();
$guestbook = new Foo_Model_Guestbook();
Copy after login

Resources in modules are automatically loaded

Zend Framework's MVC encourages the use of "modules", Modules usually have some resource type by default, and Zend Framework provides a standard directory layout for modules. In this paradigm, resource autoloaders are very useful and they are enabled by default.

Basic directory structure of the module:

<modulename>
configs/
application.ini
controllers/
helpers/
forms/
layouts/
       filters/
        helpers/
         scripts/
      models/
     services/
         filters/
                                              of of    off’s’’’’’        ’’’’'''''’’’’’’’’’’’’’’’’’’’’’’’’’'''''''''''''''‐‐''''' through through‐' through through through through through through through‐‐‐‐‐ to # Bootstrap.php

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

I hope this article will be helpful to everyone in PHP programming.

For more articles related to Zend Framework tutorial Resource Autoloading usage examples, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Using Zend Framework with PHP: Quick Start Guide Using Zend Framework with PHP: Quick Start Guide Jun 21, 2023 am 08:58 AM

Using Zend Framework with PHP: Quick Start Guide

Zend Framework middleware: Adds OAuth and OpenID login support to applications Zend Framework middleware: Adds OAuth and OpenID login support to applications Jul 28, 2023 pm 01:09 PM

Zend Framework middleware: Adds OAuth and OpenID login support to applications

Efficient database query through Zend Framework middleware Efficient database query through Zend Framework middleware Jul 28, 2023 pm 01:13 PM

Efficient database query through Zend Framework middleware

Zend Framework middleware: Add social login functionality to web applications Zend Framework middleware: Add social login functionality to web applications Jul 28, 2023 pm 07:21 PM

Zend Framework middleware: Add social login functionality to web applications

Zend Framework middleware: Add Alipay and WeChat payment functions to applications Zend Framework middleware: Add Alipay and WeChat payment functions to applications Jul 28, 2023 pm 08:01 PM

Zend Framework middleware: Add Alipay and WeChat payment functions to applications

What are the common Zend Framework 2 operations in PHP programming? What are the common Zend Framework 2 operations in PHP programming? Jun 12, 2023 am 09:01 AM

What are the common Zend Framework 2 operations in PHP programming?

CodeIgniter vs Zend Framework: Which framework is better for developing ERP systems? CodeIgniter vs Zend Framework: Which framework is better for developing ERP systems? Jun 19, 2023 am 08:53 AM

CodeIgniter vs Zend Framework: Which framework is better for developing ERP systems?

How to use Zend Framework 2 with PHP programming? How to use Zend Framework 2 with PHP programming? Jun 12, 2023 am 08:20 AM

How to use Zend Framework 2 with PHP programming?

See all articles