ThinkPHP Application Mode Extension

Source: Internet
Author: User

ThinkPHP Application Mode Extension

The ThinkPHP application model provides the opportunity to transform the core framework so that your applications can adapt to more environments and different needs. Each application mode has its own mode definition file. Compared with ThinkPHP3.1 and ThinkPHP3.2, the application mode extension is clearer and clearer, the Cli, Lite, Thin, AMF, PHPRPC, and REST modes are defined in ThinkPHP3.1. Their definition methods are similar to those in ThinkPHP3.2. If necessary, refer to the modification method, the Cli mode is built in by the ThinkPHP framework and can be used normally without separately defining the Cli mode. For more detailed adjustments, see the Cli running mode extension compiled in version 3.1. ThinkPHP also provides a convenient development environment and formal environment mode switch. Let's follow ThinkPHP's running process to parse the mystery of Its Application Mode extension.

I. Application Mode

Before studying application mode extension, let's take a look at how to use the application mode. Generally, the constant APP_MODE is defined as the application mode name in the entry file. However, when analyzing the ThinkPHP framework entry file, we know that the framework uses the common mode by default ), in addition, it can automatically identify the sae environment. Of course, if the APP_MODE constant is not defined, ThinkPHP can automatically identify the CLI and CGI modes, in addition, the ThinkPHP framework running in the CLI and CGI environments automatically makes minor adjustments to the two environments in the default mode. Of course, you can also expand these two application modes on your own.

If (function_exists ('saeautoloader ') {// automatically identifies the SAE environment defined ('app _ mode') or define ('app _ mode', 'sae '); defined ('Storage _ type') or define ('Storage _ type', 'sae ');} else {defined ('app _ mode ') or define ('app _ mode', 'common'); // The default application MODE is defined ('Storage _ type') or define ('Storage _ type ', 'file'); // The default storage type is File}


II. Application Mode Definition

In the ThinkPHP framework, except the ThinkPHP framework entry and framework boot class, basically all other functions can be changed and extended through the application mode. If we want to add an application mode, you only need to define a Mode definition file under the ThinkPHP \ Mode directory. We can learn by analyzing the common Mode.

// File path: ThinkPHP/Mode/common. php/*** ThinkPHP normal mode definition * defines a mode file. You only need to return an array containing files in the mode. * The array contains four types of extended file lists: * config is the default loading configuration file list * alias is the core class library alias configuration list * core functions to be loaded and class file list * tags behavior configuration list ** if you add load a custom class, the namespace of the custom class must be Think */return array (// configuration file 'config' => array (THINK_PATH. 'conf/convention. php', // system convention to configure CONF_PATH. 'config. php ', // public application configuration), // alias definition 'Alias' => array ('Think \ log' => CORE_PATH. 'log '. EXT, 'Think \ Log \ Driver \ file' => CORE_PATH. 'Log/Driver/file '. EXT, 'Think \ exception' => CORE_PATH. 'exception '. EXT, 'Think \ model' => CORE_PATH. 'model '. EXT, 'Think \ db' => CORE_PATH. 'db '. EXT, 'Think \ template' => CORE_PATH. 'template '. EXT, 'Think \ cache' => CORE_PATH. 'cache '. EXT, 'Think \ Cache \ Driver \ file' => CORE_PATH. 'cache/Driver/file '. EXT, 'Think \ store' => CORE_PATH. 'storage '. EXT,), // function and class file 'core' => array (THINK_PATH. 'Common/functions. php', COMMON_PATH. 'Common/function. php ', CORE_PATH. 'hook '. EXT, CORE_PATH. 'app '. EXT, CORE_PATH. 'dispatcher '. EXT, // CORE_PATH. 'log '. EXT, CORE_PATH. 'route '. EXT, CORE_PATH. 'controller '. EXT, CORE_PATH. 'view '. EXT, BEHAVIOR_PATH. 'buildlitebehavior '. EXT, BEHAVIOR_PATH. 'parsetemplatebehavior '. EXT, BEHAVIOR_PATH. 'contentreplacebehavior '. EXT,), // Behavior extension definition 'tags' => array ('app _ init' => array ('behavior \ BuildLiteBehavior ', // generate the Lite file for running ), 'app _ begin' => array ('behavior \ ReadHtmlCacheBehavior ', // read static cache), 'app _ end' => array ('behavior \ ShowPageTraceBehavior ', // page Trace display), 'view _ parse' => array ('behavior \ ParseTemplateBehavior ', // template parsing supports PHP, built-in template engine, and third-party template engine ), 'template _ filter' => array ('behavior \ ContentReplaceBehavior ', // template output replacement), 'view _ filter' => array ('behavior \ WriteHtmlCacheBehavior ', // write static cache ),),);


After we see this common application mode code, we have a little idea about ThinkPHP's application mode extension, but I still know it, but I don't know why, how does one define a file loading list and configuration change the core of the framework? The secret is in the ThinkPHPK guiding class. Let's review the following!

// Determine whether a core exists. php configuration file (this is the running mode temporarily defined by the development environment, which I understand so) // No one loads the mode file defined by APP_MODE $ mode = include is_file (CONF_PATH. 'Core. php ')? CONF_PATH. 'Core. php ': MODE_PATH.APP_MODE. '. php '; // the list of core files defined by core in the loading mode foreach ($ mode ['core'] as $ file) {if (is_file ($ file )) {include $ file; if (! APP_DEBUG) $ content. = compile ($ file) ;}// the config configuration file list defined in the loading mode foreach ($ mode ['config'] as $ key => $ file) {is_numeric ($ key )? C (include $ file): C ($ key, include $ file);} // read the configuration file if ('common '! = APP_MODE & is_file (CONF_PATH. 'config _'. APP_MODE. '. php ') C (include CONF_PATH. 'config _'. APP_MODE. '. php '); // In loading mode, the alias List defines if (isset ($ mode ['Alias']) {self :: addMap (is_array ($ mode ['Alias'])? $ Mode ['Alias']: include $ mode ['Alias']);} // load the application alias definition file if (is_file (CONF_PATH. 'Alias. php ') self: addMap (include CONF_PATH. 'Alias. php '); // defines the tags behavior in loading mode. if (isset ($ mode ['tags']) {Hook :: import (is_array ($ mode ['tags'])? $ Mode ['tags']: include $ mode ['tags']);} // load the application behavior definition if (is_file (CONF_PATH. 'tags. php ') // allows the application to add the development mode configuration definition Hook: import (include CONF_PATH. 'tags. php '); // load the underlying Language Pack L (include THINK_PATH. 'lang /'. strtolower (C ('default _ LANG ')). '. php ');


Use the code in ThinkPHP: start () to define the meaning and implementation methods of a file in a perfectly seamless mode.

3. Define a simple Running Mode

There is a Mode extended instance in the manual. You can analyze it here to define a lite simple running Mode. First, create a lite. php file in the ThinkPHP/Mode directory and define the content as follows:

Return array (// configuration file 'config' => array (THINK_PATH. 'conf/convention. php', // system convention to configure CONF_PATH. 'config. php ', // public application configuration), // alias definition 'Alias' => array ('Think \ exception' => CORE_PATH. 'exception '. EXT, 'Think \ model' => CORE_PATH. 'model '. EXT, 'Think \ db' => CORE_PATH. 'db '. EXT, 'Think \ cache' => CORE_PATH. 'cache '. EXT, 'Think \ Cache \ Driver \ file' => CORE_PATH. 'cache/Driver/file '. EXT, 'Think \ store' => CORE_PATH. 'storage '. EXT,), // function and class file 'core' => array (MODE_PATH. 'lite/functions. php', COMMON_PATH. 'Common/function. php', MODE_PATH. 'lite/app '. EXT, MODE_PATH. 'lite/Dispatcher '. EXT, MODE_PATH. 'lite/controller '. EXT, MODE_PATH. 'lite/view '. EXT, CORE_PATH. 'behavior '. EXT,), // Behavior extension definition 'tags' => array ('view _ parse' => array ('behavior \ ParseTemplate ', // template parsing supports PHP, built-in template engine, and third-party template engine), 'template _ filter' => array ('behavior \ ContentReplace ', // replace template output ),),);


From the above configuration, we found that most of the core files are replaced. Of course, the program functions to be replaced need to be implemented by ourselves, however, we recommend that you directly copy the core files defined in normal mode and modify them. Next we will implement the following core class library extension file App. class. php In ThinkPHP application development.

Create a Lite directory under the ThinkPHP/Mode directory and create the App. class. php file under the lite directory. The following is the implementation of the program file:

// The mode extension class library must be the Think namespace Think;/*** ThinkPHP application class executes the application process management Lite mode extension class * to implement the ThinkPHP core class library extension, try to simulate the implementation of the original Class Library (unless you have a special understanding of the ThinkPHP framework source code) * because a method in the extended core class file may be called in other non-extended core files, unless you want to extend all */class App {/*** application initialization * @ access public * @ return void */static public function init () {// reality}/*** run the application * @ access public * @ return void */static public function exec () {// specific implementation}/*** quick method for running the application instance entry file * @ access public * @ return void */static public function run () {// specific implementation} static public function logo () {// specific implementation }}

After implementation of all extension files in the file, you can define the constant APP_MODE as lite in the framework entry file.

The following official comments are as follows: in this manual, we need to define the MODE_NAME constant to change the running mode. To ensure accuracy, we have no choice but to search for MODE_NAME in all the ThinkPHP framework files, no results left a hanging heart. This is the method used to define the running mode in the previous version 3.1. The Manual updates the details (declare that the manual I am using is the version 3.2.12014-February 14 ).


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.