Basic Model rules and usage in ZendFramework tutorial

Source: Internet
Author: User
This article mainly introduces the basic rules and usage of the Model in the ZendFramework tutorial, and analyzes in detail the principles and usage skills of the Model in ZendFramework based on the instance form, for more information about the basic Model rules and usage of the Zend Framework tutorial, see the examples in this article. We will share this with you for your reference. The details are as follows:

Here we will talk about the model in Zend. In fact, the Model processing in Zend is quite simple.

This is mainly because of the autoload function. Unlike other frameworks, it defines complex base classes for models.

To define a model, you have to inherit a base class of the model to use the specific functions.

Zend does not encapsulate the model.

The reason is probably that the Model is mainly related to the specific business logic, and too much encapsulation will only be superfluous.

Zend uses the autoload and namespace functions, which effectively solves this problem.

Create a zendframework project model_demo1

To view errors conveniently, enable/model_demo1/application/configs/application. ini in the configuration file:

phpSettings.display_startup_errors = 1phpSettings.display_errors = 1resources.frontController.params.displayExceptions = 1

Next, let's briefly talk about the model in zend:

1. default Model

A standard webapp contains a directory such as application/models. It is easy to see that models is used to store the model of your app

The strength of this directory is that if you define a specific class in the models directory. Zend will automatically help us load data. of course, we must follow certain conventions on the premise that:

For example, you can use the zf command line to create a Model named Test.

zf create model Test

Creating a model at/www/model_demo1/application/models/Test. php

Updating project profile '/www/model_demo1/. zfproject. XML'

REFRESH The Project Directory. the following file/model_demo1/application/models/Test. php is added.

The file content is as follows:

 

It is easy to see that we should follow the following rules to use the Model:

1). It starts with Application_Model _ and is followed by the class name of the custom model.

That is, the directory structure of the web application model is/model_demo1/application/models/Test. php.

The corresponding namespace is Application_Model_Test.

Application

Models corresponds to models

Test is the name of the class file of the model.

The class name follows the constraints: class Application_Model_Test {

It is not difficult to understand Application_Model _. such rules follow the zend framework autoload and namespace conventions.

2). Application namespace

In fact, Application is also the namespace of the Application we configured in the configuration file.

If you change the appnamespace = "Application" of the configuration file to appnamespace = "App ".

Our original program will report an error. The reason is self-evident. Therefore, zend is not so intelligent.

If you want to investigate the principle in detail, the following classes are used to complete this function:

Zend_Application_Bootstrap_BootstrapZend_Application_Module_Autoloader

2. custom namespace

Zend is the default namespace. For example, create a class Zend_Test in/model_demo1/library/Zend/Test. php.

  '; }}

It can be used in programs without any operation. Example: Zend_Test: echoZendTest ();
Here are two methods to customize a namespace:

1). use the application. ini configuration file

Default namespace

appnamespace = "Application"

Custom namespace

autoloadernamespaces.app = "App_"autoloadernamespaces.my = "MyApp_"

Or

autoloadernamespaces[] = "App_"autoloadernamespaces[] = "MyApp_"

Implementation class: Zend \ Application. php

Public function setOptions (array $ options) {if (! Empty ($ options ['config']) {if (is_array ($ options ['config']) {

This article describes the basic rules and usage of the Model in the Zend Framework tutorial. We will share this with you for your reference. The details are as follows:

Here we will talk about the model in Zend. In fact, the Model processing in Zend is quite simple.

This is mainly because of the autoload function. Unlike other frameworks, it defines complex base classes for models.

To define a model, you have to inherit a base class of the model to use the specific functions.

Zend does not encapsulate the model.

The reason is probably that the Model is mainly related to the specific business logic, and too much encapsulation will only be superfluous.

Zend uses the autoload and namespace functions, which effectively solves this problem.

Create a zendframework project model_demo1

To view errors conveniently, enable/model_demo1/application/configs/application. ini in the configuration file:

phpSettings.display_startup_errors = 1phpSettings.display_errors = 1resources.frontController.params.displayExceptions = 1

Next, let's briefly talk about the model in zend:

1. default Model

A standard webapp contains a directory such as application/models. It is easy to see that models is used to store the model of your app

The strength of this directory is that if you define a specific class in the models directory. Zend will automatically help us load data. of course, we must follow certain conventions on the premise that:

For example, you can use the zf command line to create a Model named Test.

zf create model Test

Creating a model at/www/model_demo1/application/models/Test. php

Updating project profile '/www/model_demo1/. zfproject. XML'

REFRESH The Project Directory. the following file/model_demo1/application/models/Test. php is added.

The file content is as follows:

   

It is easy to see that we should follow the following rules to use the Model:

1). It starts with Application_Model _ and is followed by the class name of the custom model.

That is, the directory structure of the web application model is/model_demo1/application/models/Test. php.

The corresponding namespace is Application_Model_Test.

Application

Models corresponds to models

Test is the name of the class file of the model.

The class name follows the constraints: class Application_Model_Test {

It is not difficult to understand Application_Model _. such rules follow the zend framework autoload and namespace conventions.

2). Application namespace

In fact, Application is also the namespace of the Application we configured in the configuration file.

If you change the appnamespace = "Application" of the configuration file to appnamespace = "App ".

Our original program will report an error. The reason is self-evident. Therefore, zend is not so intelligent.

If you want to investigate the principle in detail, the following classes are used to complete this function:

Zend_Application_Bootstrap_BootstrapZend_Application_Module_Autoloader

2. custom namespace

Zend is the default namespace. For example, create a class Zend_Test in/model_demo1/library/Zend/Test. php.

    '; }}

It can be used in programs without any operation. Example: Zend_Test: echoZendTest ();
Here are two methods to customize a namespace:

1). use the application. ini configuration file

Default namespace

appnamespace = "Application"

Custom namespace

autoloadernamespaces.app = "App_"autoloadernamespaces.my = "MyApp_"

Or

autoloadernamespaces[] = "App_"autoloadernamespaces[] = "MyApp_"

Implementation class: Zend \ Application. php

___FCKpd___8

2). in the Bootstrap. php file

For example,/model_demo1/application/Bootstrap. php

    getApplication ();  $namespaces = array (    'AppTest'  );  $app->setAutoloaderNamespaces ( $namespaces );  return $app; }}

/Model_demo1/library/AppTest/Test. php

    '; }}

/Model_demo1/application/controllers/IndexController. php

AppTest_Test::echoAppTestTest();

3). use a specific class to complete automatic loading

$auto_loader = Zend_Loader_Autoloader::getInstance();$resourceLoader = new Zend_Loader_Autoloader_Resource(array(  'basePath' => '/www/model_demo1/application',  'namespace' => '',  'resourceTypes' => array(    'model' => array(      'path' => 'models',      'namespace' => 'Model'    )  )));$auto_loader->pushAutoloader($resourceLoader);$auto_loader->registerNamespace(array('AppTest2_'));AppTest2_Test::echoAppTest2Test();Model_ModelTest::echoModelModelTest();

/Model_demo1/application/models/ModelTest. php

    '; }}

/Model_demo1/library/AppTest2/Test. php

    '; }}

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.