The Mystery of thinkphp application pattern extension
The thinkphp Application model provides the opportunity to transform the core framework, allowing your application to adapt to more environments and different needs. Each application pattern has its own schema definition file, relative to the ThinkPHP3.1 version, the ThinkPHP3.2 version of the extension of the application mode is more clear and clear, in the ThinkPHP3.1 version defined 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 changes, where the CLI mode is built into the thinkphp framework, without the need to define the CLI mode can be used normally, such as a more detailed adjustment can refer to the 3.1 version of the CLI run mode extension. Thinkphp also provides a convenient development environment and a formal environment for mode switching. Let's resolve the puzzle of its application pattern expansion with the thinkphp running process.
First, the use of application mode
Before you study the application pattern extension, look at how to use the application pattern. In general, by defining the constant App_mode in the portal file to apply the schema name, but when parsing the thinkphp framework portal file, it is understood that the framework default adoption mode is normal mode (common), and the SAE environment can be automatically recognized, if not defined App_mode constants, Of course thinkphp can automatically recognize the CLI and CGI patterns, and running in the CLI and CGI environments The thinkphp framework automatically makes minor adjustments to both environments in the default mode, and can of course extend both of these applications.
if (function_exists (' Saeautoloader ')) {//auto-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 default to File }
Second, the application pattern definition
In the thinkphp framework, in addition to the thinkphp framework portal and the framework guidance class, all the basic other functions can be changed and extended through the application mode, if we want to add an application mode, only need to define a schema definition file under the Thinkphp\mode directory, We can learn by analyzing the common model.
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 * In the array contains a list of 4 extension files: * config is the default load Set File list * Alias for Core class Library alias configuration list * Core required to load kernel functions and class file list * tags behavior configuration list * * If you load a custom class in the app schema definition, the namespace of that custom class must be think */re Turn array (//config file ' config ' = = Array (think_path. ' conf/convention.php '),//System custom configuration Conf_path. ' config.php ',//Apply public config),//alias definition ' Alias ' = = Array (' Think\log ' = Core_pa TH. ' 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,),//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\buildlitebehavio R ',//Generate run Lite file), ' 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, the built-in template engine, and the third-party template engine), ' template_filter ' = = Array (' Behavior\contentreplacebehavior ', Template output substitution), ' view_filter ' = = Array (' Behavior\writehtmlcachebehavior ',//write static cache) , ),);
After we see this common application pattern code, it's a little bit clear how the thinkphp application pattern extension is going to work, but still know it somehow, define how a load file list and configuration changes the core of the framework? The secret is in the THINKPHPK boot class, let's review the following again!
Determine if there is a core.php profile (this is a temporary run mode defined by the development environment, as I understand it)//No person loads the schema file defined by the App_mode $mode = include is_file (conf_pat H. ' core.php ')? Conf_path. ' core.php ': Mode_path. App_mode. '. PHP '; List of core files defined in the load mode foreach ($mode [' core '] as $file) {if (Is_file ($file)) {include $ File if (! App_debug) $content. = Compile ($file); }}//The Config profile list defined in Load mode foreach ($mode [' config '] as $key = + $file) {Is_numeri C ($key)? C (include $file): C ($key, include $file); }//Read the configuration file corresponding to the current application mode if (' common '! = App_mode && is_file (conf_path. ' Config_ '). App_mode. '. php ') C (include Conf_path. ' Config_ '). App_mode. '. php '); The alias alias list in the load mode defines if (Isset ($mode [' Alias ')]) {Self::addmap ("Is_array ($mode [' Alias '])? $mode [' Alias ']: Include $mode [' Alias ']); }//Load app alias definition file if (is_file (conf_path. ' alias.php ')) Self::addmap (include Conf_path. ' alias.php '); Load mode tags behavior definition if (isset ($mode [' tags '])) {Hook::import (Is_array ($mode [' tags '])? $mode [' Tags ']:inclu De $mode [' tags ']); }//Load app behavior definition if (is_file (conf_path. ' tags.php '))//Allow app to add development mode configuration definition Hook::impor T (include Conf_path. ' tags.php '); Load the framework's underlying language pack L (include Think_path. ' lang/'. Strtolower (C (' Default_lang ')). php ');
With this code in Thinkphp::start (), the perfect seamless correlation pattern defines the meaning of the file and how it is implemented.
Third, the definition of simple operation mode
The manual has a pattern to extend to the instance, can get here to analyze, define a lite concise operation mode, first in the Thinkphp/mode directory, create a new lite.php file content defined as follows:
return Array (//config file ' config ' = = Array (think_path. ' conf/convention.php ',//System custom Configuration Conf_path. ' config.php ',//Apply public config),//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,),//Function and class file ' core ' = = Array (mode_path. ' lite/functions.php ', Common_path. ' Com Mon/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,), The behavior extension defines the ' tags ' = = Array (' view_parse ' = = Array (' Behavior\parsetemplat E ',//template parsing supports PHP, built-in template engine and third-party template engine), ' template_filter ' = = Array (' Behavio R\contentreplace ',//template output replacement),);
From the above configuration we found that core files are mostly replaced, of course, these need to be replaced by the function of the program needs to be implemented by ourselves, but it is recommended that you directly copy the normal schema defined in the core files to modify. Next, we will implement the following core class library extension files in thinkphp application development App.class.php
Create a lite directory under the Thinkphp/mode directory and build the App.class.php file in the Lite directory, the following is the implementation of the Program files:
The schema Extension class library must be a Think namespace namespace Think; /** * thinkphp Application Class execution application process Management Lite mode extension class * When implementing the thinkphp Core Class library extension, imitate the original class library implementation (unless you have a special knowledge of the thinkphp Framework source code) * Because in other non-extensible core files you might call a method in the extended core class file unless you intend to extend all */class app{/** * Application Initialization * @access public * @return void */static public fun Ction init () { ///Concrete Reality}/** * Execute application * @access public * @return void */static public Function exec () { //specific implementation}/** * Shortcut method used to run the application instance portal file * @access public * @return void */static public Function run () { //implementation} static public function L Ogo () { //Specific Implementation}}
When the file is implemented for all extension files, you can define the App_mode constant as lite in the framework portal file.
Here the groove below the official: This section of the manual is actually to define the Mode_name constant to change the operating mode, in order to accuracy helpless in the thinkphp framework all files search Mode_name, the result is not put down a hanging heart. This is the method of defining the operating mode in the previous 3.1 release, the manual update details (I'm using the manual that I'm working on, 3.2.12014, February 14).