ThinkPHP Application Mode extension details

Source: Internet
Author: User
This article mainly introduces ThinkPHP Application Mode extension. For more information, see

This article mainly introduces ThinkPHP Application Mode extension. For more information, see

The ThinkPHP application model makes it easier for developers to transform their core frameworks and adapt your applications 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:

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.