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:
<?phpclass Application_Model_Test{}
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.
<?phpclass Zend_Test{ static public function echoZendTest(){ echo 'ZendTest
'; }}
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'])) { $_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
For example,/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 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
<?phpclass Model_ModelTest{ static function echoModelModelTest(){ echo 'Model_ModelTest
'; }}
/Model_demo1/library/AppTest2/Test. php
<?phpclass AppTest2_Test{ static public function echoAppTest2Test(){ echo 'AppTest2Test
'; }}