Zend Framework Tutorial Model basic rules and usage methods, zendframework_php tutorial

Source: Internet
Author: User

Zend Framework Tutorial Model Basic rules and methods of use, Zendframework


In this paper, we describe the basic rules and usage methods of Zend Framework tutorial model. Share to everyone for your reference, as follows:

This is about the model in Zend. In fact, the model processing in Zend is quite simple.

This is mainly due to the autoload function. Unlike other frameworks, a complex base class is defined for the model.

If you want to define the model, you have to inherit a model base class before you can use the specific functionality.

The model is not encapsulated in the Zend.

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

Zend uses the AutoLoad and namespace functions, which is a very tactful solution to this problem.

Create a Zendframework project Model_demo1

To make it easier to see the error, we can/model_demo1/application/configs/application.ini open the error message switch in the configuration file as follows:

Phpsettings.display_startup_errors = 1phpsettings.display_errors = 1resources.frontcontroller.params.displayexceptions = 1

The following is a brief talk about the model in Zend:

1. The default model

A standard webapp will have directories such as Application/models. It's not hard to see the model that models used to store your app

The power of this directory is that if you define a specific class in the models directory. Zend will automatically help us to load, of course, to follow a certain agreement, provided that:

For example, create a model named Test with the ZF command line

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 to see that the following files have been added/model_demo1/application/models/test.php

The contents of the file are as follows:

<?phpclass application_model_test{}

It is not difficult to see that we use model to follow the following rules:

1). Start with Application_model_, and then follow the class name for the custom Model.

That is: The directory structure of our Web application's model is/model_demo1/application/models/test.php

The corresponding namespace is application_model_test.

Application Correspondence Application

Models Correspondence Models

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

The name of the class is constrained by the following: Class Application_model_test {

It is not difficult to understand application_model_, such rules follow the AutoLoad and namespace conventions of the Zend Framework.

2). Application namespaces

In fact, application is also the namespace of the app that we configured in the configuration file.

If the Appnamespace = "Application" of the configuration file is modified to Appnamespace = "App".

Our original procedure will be an error. The reason is self-evident. So Zend is not so smart.

If you want to investigate the principle in detail, it is probably the following class to complete this function:

Zend_application_bootstrap_bootstrapzend_application_module_autoloader

2. Customizing the Namespace

Zend is the default namespace. For example, create a class in/model_demo1/library/zend/test.php zend_test

<?phpclass zend_test{static public Function echozendtest () {  echo ' zendtest
'; }}

You do not need to do anything to use it in your program. For example: Zend_test::echozendtest ();
Here's a brief description of the two methods of customizing the namespace:

1). Using the Application.ini configuration file

Default namespace

Appnamespace = "Application"

Custom namespaces

Autoloadernamespaces.app = "App_" autoloadernamespaces.my = "Myapp_"

Or

Autoloadernamespaces[] = "App_" autoloadernamespaces[] = "Myapp_"

The concrete implementation class is: zend\application.php

Public function setoptions (array $options) {if (!empty ($options [' config ')]) {  if (Is_array ($options [' config '])) { c1/>$_options = Array ();   foreach ($options [' config '] as $tmp) {    $_options = $this->mergeoptions ($_options, $this->_loadconfig ($tmp)) ;   }   $options = $this->mergeoptions ($_options, $options);  } else {   $options = $this->mergeoptions ($this->_loadconfig ($options [' config ']), $options);}  } $this- >_options = $options; $options = Array_change_key_case ($options, case_lower); $this->_optionkeys = Array_keys ($options); if (!empty ($options [' phpsettings '])) {  $this->setphpsettings ($options [' phpsettings ']);} if (!empty ($options [' includepaths '])) {  $this->setincludepaths ($options [' includepaths ']);} if (!empty ($options [' autoloadernamespaces ']) {  $this->setautoloadernamespaces ($options [' autoloadernamespaces ']);}

2). In the bootstrap.php file

such as/model_demo1/application/bootstrap.php

<?phpclass Bootstrap extends Zend_application_bootstrap_bootstrap {protected function _initautoload () {  $app = $ This->getapplication ();  $namespaces = Array (    ' apptest '  );  $app->setautoloadernamespaces ($namespaces);  return $app; }}

/model_demo1/library/apptest/test.php

<?phpclass apptest_test{static public Function echoapptesttest () {  echo ' apptesttest
'; }}

/model_demo1/application/controllers/indexcontroller.php

Apptest_test::echoapptesttest ();

3). Use specific classes 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

<?phpclass model_modeltest{static function Echomodelmodeltest () {  echo ' model_modeltest
'; }}

/model_demo1/library/apptest2/test.php

<?phpclass apptest2_test{static public Function echoapptest2test () {  echo ' apptest2test
'; }}

More interested in Zend related content readers can view the topic: "Zend framework of the introductory tutorial", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Tutorial", "PHP object-oriented Programming introduction tutorial "," Introduction to Php+mysql Database Operation "and" PHP common database Operation Skills Summary "

I hope this article is helpful to you in PHP programming.

Articles you may be interested in:

    • Zend Framework Tutorial Zend_layout Layout Assistant Detailed
    • Methods of using Memcache in the Zend Framework
    • Resolution of URL case problem in Zend Framework frame
    • Zend Framework 2.0 Event Manager (the EventManager) Getting Started tutorial
    • Zend Framework Page Cache instance
    • Very useful Zend Framework paging class
    • Layout in the Zend Framework (modular layout) detailed
    • Zend Framework Configuration Operations Database instance analysis
    • Windows Zendframework Project Environment Setup (via command line configuration)
    • Zend Framework Tutorial Model Usage Simple Example

http://www.bkjia.com/PHPjc/1106113.html www.bkjia.com true http://www.bkjia.com/PHPjc/1106113.html techarticle Zend Framework Tutorial Model basic rules and usage methods, zendframework This article describes the Zend Framework tutorial model Basic rules and methods of use. Share for the big ...

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