thinkphp Application Mode extension Detailed _php example

Source: Internet
Author: User

Thinkphp's application model makes it easier for developers to transform the core framework and allows your application to adapt to more environments and different needs. Each application pattern has its own schema definition file, and in contrast to the ThinkPHP3.1 version, the ThinkPHP3.2 version is clearer and clearer about the extension of the application pattern, defined in the ThinkPHP3.1 version of the CLI, Lite, Thin, AMF, Phprpc, Rest mode, It is defined in the same way as the ThinkPHP3.2 version, if necessary can refer to the modification, where the CLI mode is built into the thinkphp framework, do not have to define the CLI mode can be used for normal use, such as the need for more detailed adjustment can refer to version 3.1 of the CLI run mode extension. Thinkphp also provides a convenient development environment and a formal environment for mode switching. Let us resolve the mystery of its application pattern extension as Thinkphp's running process.

The use of application mode

Before you study the application pattern extension, see how to use the application mode. In general, by defining a constant App_mode for the entry file to apply the schema name, when parsing the thinkphp framework entry file, you learn that the framework defaults to normal mode (common) and that the SAE environment is automatically recognized, provided that the App_mode constant is not defined, Of course, thinkphp can automatically recognize the CLI and CGI patterns, and running the thinkphp framework in the CLI and CGI environments automatically makes minor adjustments to both environments in the default mode, and can, of course, extend both of these application modes.

if (function_exists (' Saeautoloader ')) {//Automatically identify 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 ');//Application Mode defaults to normal mode  
  defined (' Storage_type ') or define (' Storage_type ',  ' file ');//storage type defaults to File  
}

Second, the application pattern definition

In the thinkphp framework, in addition to the thinkphp framework portal and the framework boot class, all other functions can be changed and extended through Application mode, and if we want to add an application pattern, simply define a schema definition file under the Thinkphp\mode directory. We can learn by analyzing the common model. The code for the file is as follows:

File path: thinkphp/mode/common.php/** * thinkphp Normal Mode definition * Defines a schema file that only needs to return an array of schema-containing files * There are 4 main extensions in the array list: * config is the default Load Profile list * Alias for Core class Library alias configuration list * Core needs to load kernel function and class file list * tags behavior configuration list * * If you load a custom class in the application pattern definition, the namespace for that custom class must be think * /return Array (//config file ' config ' => array (think_path. ' conf/convention.php '),//System Convention configuration Conf_path. ' Config. PHP ',//Apply public 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\storage ' => core_path. ' Storage '. EXT,),//Functions and Class files ' 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 run Lite text Pieces), ' App_begin ' => array (' Behavior\readhtmlcachebehavior ',//read static cache), ' App_end ' =&gt ; Array (' Behavior\showpagetracebehavior ',//page trace display), ' View_parse ' => Array (' Behavior\parsetem Platebehavior ',//template parsing supports PHP, built-in template engine and third party template engine, ' Template_filter ' => Array (' Behavior\contentreplacebehavi or ',//template output replacement), ' View_filter ' => Array (' BEHAVIOR\WRITEHTMLCACHebehavior ',//write static cache),);

 

When we see this generic application pattern code, it's kind of clear how thinkphp's application-mode extensions are going, but still don't know why, but how do you define a list of loading files and how the configuration changes the framework core? The secret is in the Thinkphp Guide class, Let's go back to the following!

Determine if there is a core.php configuration file (This is a temporary definition of the development environment, as I understand it)//no person loads the App_mode defined schema file $mode = include Is_file (conf_path. ' CORE.P HP ')? Conf_path. ' core.php ': Mode_path. App_mode. '.
     PHP ';
        List of core files in kernel definition in load mode foreach ($mode [' core '] as $file) {if (Is_file ($file)) {include $file; if (!
       App_debug) $content. = Compile ($file); A list of config profiles defined in//load mode foreach ($mode [' config '] as $key => $file) {is_numeric ($key)?
     C (include $file): C ($key, include $file); //Read the configuration file for the current application mode if (' Common '!= app_mode && is_file (conf_path. ' Config_ '). App_mode. '. php ')) C (include Conf_path. ' Config_ '). App_mode. '. 
 
     php '); In load mode, the alias alias list defines if (Isset ($mode [' Alias '])) {Self::addmap ($mode [' Alias '])? $mode [' Alias ']:include $m
     ode[' Alias ']);
 
     ///Load application alias definition file if (is_file (conf_path. ' alias.php ')) Self::addmap (include Conf_path. ' alias.php ');
  tags behavior definition if (isset ($mode [' tags ']) in load mode {     Hook::import (Is_array ($mode [' tags '])? $mode [' Tags ']:include $mode [' tags ']); }//Load application behavior definition if (is_file (conf_path. ' tags.php '))//Allow application to add Development mode configuration definition Hook::import (include Conf_path .' 
 
     Tags.php '); Load framework underlying language pack L (include Think_path. ' lang/'. Strtolower (C (' Default_lang ')).

 php ');

Through this code in Thinkphp::start (), the meaning and implementation method of the schema definition file are perfectly seamless.

Three, define simple operation mode

The manual has a pattern extended to the example, you can get here to analyze, define a lite simple run mode, first in the Thinkphp/mode directory to create a new lite.php file content defined as follows:

return Array (//config file ' config ' => array (think_path. ' conf/convention.php ',//system Convention Configuration Conf_path. ') Config.php ',//Apply public 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\storage ' => core_path. ' Storage '. EXT,),//Functions and Class files ' 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 solution Analysis supports PHP,Built-in template engine and third party template engine), ' Template_filter ' => array (' behavior\contentreplace ',//template output replacement),

 ),
);

From the configuration above we found that most of the core files are replaced, of course, these need to be replaced by the program features we need to implement, but we recommend that you directly copy the normal mode of the definition of the core files to modify. Next we will implement the core class library extension files in the following thinkphp application development App.class.php

Create a lite directory under the Thinkphp/mode directory and create App.class.php files in the Lite directory, following is the implementation of the program file:

The schema Extension class library must be the namespace of the "I" namespace
.
 
/**
 * thinkphp Application class executes application Process Management Lite Schema Extension class
 * To achieve thinkphp Core class library extensions, copy as much as possible of the original class library implementation (unless you specifically understand the thinkphp Framework source code)
 * Because a method in the extended core class file may be invoked in other core files that are not expanded unless you intend to extend all *
 /
class app{
/**
 * Application Initialization
 * @access public
 * @return void
 *
/static public function init () {
    //concrete Reality
}
 
/**
 * Execute Application
 * Access public
 * @return void
/static public function exec () {
    //Concrete Implementation
}
 
/**
 * The shortcut method used to run application instance portal files
 * @access public
 * @return
 void
/static public function run () {
    //specific implementation c31/>}
 
static public function logo () {
    //specific implementation
}
}

when all the file extension files are implemented, you can define the App_mode constant as lite in the frame entry file.

Also note that the manual requires defined Mode_name constants to change the way the run mode is defined in the previous 3.1 versions of the run mode , and users with the new version need to be aware of 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.