Thinkphp3.2 system operation process 1 and thinkphp3.2
If the company uses the tp framework, as a newbie, it will first be asked to understand the tp system operation process. This article aims to help new kids shoes quickly understand the tp system process. Follow the instructions in the tp Manual (the serial number is the same for your convenience ):
1. User URL request. 2. Call the application entry file (index. php is used as an example here)
Most websites use url rewriting to hide index. php. For more information, see the tp manual. First, execute index. php,
1 // application entry file 2 3 // check the PHP environment 4 if (version_compare (PHP_VERSION, '5. 3.0 ',' <') die ('require PHP> 5.3.0! '); 5 6 // enable the debugging mode. It is recommended that you enable the annotation or set it to false in the development phase. 7. define ('app _ debug', True ); 8 9 // define the application directory 10 define ('app _ path ','. /Application/'); 11 12 // introduce the ThinkPHP entry file 13 require '. /ThinkPHP. php ';
First, determine whether the php version is 5.3.0 or above, and then enable the debugging mode (as described in the following process). When defining the path, the default tp path is the relative path, however, using the absolute path will speed up the operation. The methods used in the tp manual are:
The realpath () method gets the absolute path. Here we recommend the getcwd () method, and finally enter Thinkphp in the core package. php; Note: The execution address of the entire website process is in the index of the root directory of the website. in php, do not think that the execution address has changed because the process has entered other controllers and methods. This is a misunderstanding of the process for beginners.
3. Load the framework entry file (Thinkphp. php) 4. Record the initial running time and memory overhead 5. System constant judgment and definition
Thinkphp. php will define many constants after entering. If the constants have been defined in index. php, they cannot be redefined. The following code is worth noting:
1 if(!defined('__ROOT__')) {2 $_root = rtrim(dirname(_PHP_FILE_),'/');3 define('__ROOT__', (($_root=='/' || $_root=='\\')?'':$_root));4 }
The reason is that when we use the absolute address of the root directory of the website, if we use a local file, the root directory format is '/XXX ', but after the project goes online, the address is '/', and the problem arises. The address next to '/XXX' needs to be written'/'first, if '/' is followed by another '/', it will be repeated. To avoid path conflicts between the online and local systems, replace it here. When $ _ root is, directly replace it with null, so that you can directly connect to '/' as locally.
1 // load the core Think class 2 require CORE_PATH. 'think'. EXT; 3 // The application initializes 4 Think \ Think: start ();
Next, run the start method in Think. class. php under TP3.2 \ ThinkPHP \ Library \ Think.
6. Load the Think \ Think and execute the Think: start method to initialize the application. ---- (7-23) is the same
First, register the AUTOLOAD method, set error and exception handling, and initialize the file storage method (this part does not need to be detailed at the moment ). Next, we will focus on pre-compiled files:
1 // [additional comment] defines the name of the precompiled file (starting with the application mode, the default value is common) 2 $ runtimefile = RUNTIME_PATH.APP_MODE .'~ Runtime. php'; 3 if (! APP_DEBUG & Storage: has ($ runtimefile) {4 // [Additional comments] If the debug mode is disabled and the pre-compiled file exists, load 5 Storage :: load ($ runtimefile); 6} else {7 // [Additional comments] if the debug mode is enabled and the pre-compiled file exists, delete it; 8 if (Storage :: has ($ runtimefile) 9 Storage: unlink ($ runtimefile); 10 // [Additional comments] initialize the pre-compilation parameter $ content11 $ content = ''; 12 // read application Mode 13 // [Additional comments] Step 1 --- read the application configuration file. The default value is ThinkPHP/Mode/common. php14 $ mode = include is_file (CONF_PATH. 'Core. php ')? CONF_PATH. 'Core. php ': MODE_PATH.APP_MODE. '. php '; 15 // load the core file 16 // [Additional comments] --- common. php is a two-dimensional array and starts traversing (functions and class files) 17 foreach ($ mode ['core'] as $ file) {18 if (is_file ($ file )) {19 // [Additional comments] load functions and class files 20 include $ file; 21 // [Additional comments] --- get the file content and save it to $ content 22 if (! APP_DEBUG) $ content. = compile ($ file); 23} 24} 25 26 // load the application mode configuration file 27 // [Additional comments] load_config determine different types of configuration files, and return its content 28 // [Additional comments] configuration file loaded for the first time, convention configuration file convention. php and application configuration file config. php29 // [additional comment] If $ file is an associated array, use the original key value 30 // [additional comment] To Call The C function and input the array as initialization, therefore, $ _ CONFIG update 31 // [Additional comments] This step will be skipped when you read the pre-compiled file, therefore, in deployment mode, modifying these two configuration files cannot take effect for 32 foreach ($ mode ['config'] as $ key => $ file) {33 is_numeric ($ key )? C (load_config ($ file): C ($ key, load_config ($ file )); 34} 35 36 // read the configuration file 37 corresponding to the current application mode // [additional comment] the configuration file reads the application configuration, which is valid in non-common application mode, read config_xxx.phhwif ('common '! = APP_MODE & is_file (CONF_PATH. 'config _'. APP_MODE.CONF_EXT) 39 C (load_config (CONF_PATH. 'config _'. APP_MODE.CONF_EXT); 40 41 // Loading Mode alias definition 42 // [Additional comments] loading of official alias definition, ing array updated to $ _ map43 // [Additional comments] addMap () update the value of $ _ map in the class. It is an array directly updated. if it is not an array, it is created as an array and updated $ _ map44 if (isset ($ mode ['Alias']). {45 self: addMap (is_array ($ mode ['Alias'])? $ Mode ['Alias']: include $ mode ['Alias']); 46} 47 48 // load the application alias definition file 49 // [additional comment] custom alias definition loading, update the ing array to $ _ map50 if (is_file (CONF_PATH. 'Alias. php ') 51 self: addMap (include CONF_PATH. 'Alias. php '); 52 53 // behavior definition of Loading Mode 54 // [Additional comments] official default behavior tag bound to array loading 55 // [Additional comments] only pass 1 parameter for merging import, 2 parameter: overwrite import (manual description) 56 // [additional comment] $ _ tags in the hook class has been updated 57 if (isset ($ mode ['tags']) {58 Hook: import (is_array ($ mode ['tags'])? $ Mode ['tags']: include $ mode ['tags']); 59} 60 61 // load application behavior definition 62 // [Additional comments] custom behavior tag binding array loading 63 // $ _ tags in the [Additional comments] hook class has been updated 64 if (is_file (CONF_PATH. 'tags. php ') 65 // allow the application to add the development mode configuration definition 66 Hook: import (include CONF_PATH. 'tags. php '); 67 68 // load the underlying Language Pack of the Framework 69 // [Additional Notes] L method to load the framework Language Pack $ _ lang has been updated to 70 L (include THINK_PATH. 'lang /'. strtolower (C ('default _ LANG ')). '. php '); 71 72 // [Additional comments] after the basic load is completed, the pre-compiled file 73 if (! APP_DEBUG) {74 // [Additional comments] If the debug mode is disabled 75 // [Additional comments] in the pre-compiled file, integrate the configuration parameters 76 $ content. = "\ nnamespace {Think \ Think: addMap (". var_export (self: $ _ map, true ). ");"; 77 // [Additional comments] in the pre-compiled file, integrate the language parameters, configuration parameters, and behavior binding parameters 78 $ content. = "\ nL (". var_export (L (), true ). "); \ nC (". var_export (C (), true ). '); Think \ Hook: import ('. var_export (Hook: get (), true ). ');}'; 79 // [additional comment] import the content to the pre-compiled file 80 Storage: put ($ runtimefile, strip_whitespace ('<? Php '. $ content); 81} else {82 // load the default configuration file of the system in debug mode 83 // [additional note] If debug mode is enabled, you need to load the Additional configuration file debug. php84 C (include THINK_PATH. 'conf/debug. php '); 85 // read the application debugging configuration file 86 // [additional comment] If debug. if php exists, 87 if (is_file (CONF_PATH. 'debug '. CONF_EXT) 88 C (include CONF_PATH. 'debug '. CONF_EXT); 89} 90}
The code is long and additional comments have been integrated into the code. First, make a judgment. If the debugging mode is disabled (the pre-compiled file is not generated when the debugging mode is enabled) and the file exists, load the file directly, and the subsequent judgment is not executed.
Summary: The pre-compiled file contains the following content: core in 1. common. php, 2. Part of the configuration file, 3. ing, 4. behavior, 5. Language
To be continued ......