Zend Framework Tutorial Model Basic rules and usage _php examples

Source: Internet
Author: User
Tags autoload zend zend framework

This paper illustrates the basic rules and usage of model models of Zend Framework tutorial. Share to everyone for your reference, specific as follows:

Here to talk about the model in Zend. In fact, the model processing in Zend is quite simple.

This mainly benefits from the AutoLoad function. Unlike other frameworks, you define complex base classes for the model.

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

The model is not encapsulated in the Zend.

The reason is probably the model is mainly related to the specific business logic, too much encapsulation, will only gild the lily.

Zend uses the AutoLoad and namespace functions, which is a euphemism for solving the problem.

Create a Zendframework project Model_demo1

For easy viewing of errors, we can/model_demo1/application/configs/application.ini the error message switch in the configuration file as follows:

phpsettings.display_startup_errors = 1
phpsettings.display_errors = 1
Resources.frontController.params.displayExceptions = 1

The following is a brief talk about model in Zend:

1. The default model

A standard webapp will have a directory like application/models. It's not hard to see that models is used to store your app's model

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

For example, 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 to see, add the following file/model_demo1/application/models/test.php

The contents of the document are as follows:

<?php
class Application_model_test
{}

It's not hard to see that we're using model to follow the following rules:

1. Begins with Application_model_, followed by the custom Model's class name.

namely: our Web Application Model directory structure for/model_demo1/application/models/test.php

The corresponding namespace is application_model_test.

Application corresponds to Application

Models corresponds to Models

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

The name of the class is bound by: 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 application that we configure in the configuration file.

If you modify the Appnamespace = "Application" of the configuration file to Appnamespace = "App".

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

If the principle is to be investigated in detail, it is likely that the following classes complete this function:

Zend_application_bootstrap_bootstrap
Zend_application_module_autoloader

2. Custom namespaces

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

<?php
class zend_test{
 static public Function echozendtest () {
  echo ' zendtest<br/> ';
 }
}

You do not need to do anything, you can use it in your program. For example: Zend_test::echozendtest ();
Here is a simple description of two ways to customize a namespace:

1). Using the Application.ini configuration file

Default namespaces

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 ']) {
   $_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

<?php
class 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

<?php
class apptest_test{
 static public Function echoapptesttest () {
  echo ' apptesttest<br/> ' ;
 }
}

/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 ' => '))
;
$auto _loader->pushautoloader ($resourceLoader);
$auto _loader->registernamespace (Array (' apptest2_ '));
Apptest2_test::echoapptest2test ();
Model_modeltest::echomodelmodeltest ();

/model_demo1/application/models/modeltest.php

<?php
class model_modeltest{
 static function Echomodelmodeltest () {
  echo ' model_modeltest<br/ > ';
 }
}

/model_demo1/library/apptest2/test.php

<?php
class apptest2_test{
 static public Function echoapptest2test () {
  echo ' apptest2test<br/> ' ;
 }
}

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.