ThinkPHP application mode extension details

Source: Internet
Author: User
This article mainly introduces ThinkPHP application mode extension. if you need ThinkPHP, you can refer to the ThinkPHP application mode to make it easier for developers to transform the core framework, in addition, it allows your applications to adapt to more environments and different needs. Each application mode has its own mode definition file. compared with ThinkPHP3.1, ThinkPHP3.2 makes the extension of the application mode 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. The code for this file is as follows:

// 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 ThinkPHP 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..

Note that,The MODE_NAME constant required in the manual to change the running mode is the method for defining the running mode in earlier versions 3.1.Users who use the new version must pay attention to this.

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.