ThinkPHP2.2 Framework execution principle, flowchart online Manual
thinkphp Controller Execution Flow
first URL access to a user http://<serverIp>/My/index.php/Index/show/ The process performed is analyzed in detail, and the user's URL access is first targeted to the index.php portal file of my project (note: If Url_rewrite is used, Perhaps index.php has been hidden), the entry file for the project is actually instantiating an app instance and executing the application.
1. Loading public entrance file
Before instantiating the app class, we need to first load the system's public portal file, thinkphp.php, which is the total entrance to thinkphp, let's explore it. In the process of loading the thinkphp.php file, the following actions were actually completed:
Record start execution time $GLOBALS [' _begintime '];
Detects the Think_path definition, if none is created;
Detect the project name App_name, if not, according to a certain rule automatically defined;
Detects the project compile cache directory definition, does not take the project's temp directory;
Load system Definition file defines.php and common function file functions.php;
If the project compilation cache directory does not exist, the project directory structure is created automatically;
Load the System core class library (including base, APP, Action, Model, View, Thinkexception, Log);
If PHP version is lower than 5.2.0 load compatible function library compat.php;
Generate core compilation cache ~runtime.php;
Log load file time $GLOBALS [' _loadtime '];
2. Init of Project initialization
After loading the public portal file for thinkphp, we started to execute the app, and the first thing was to initialize the app.
Set error and exception handling mechanisms (Set_error_handler and Set_exception_handler);
Project pre-compilation and onboarding;
Set time zone support;
Session filter Check;
Session initialization;
Check and load the plugin;
URL analysis and scheduling;
Gets the currently executing module and operation name;
Load module configuration file;
Page anti-refresh mechanism check;
Language check and read the corresponding language files;
The template examines and defines the relevant template variables;
RBAC permission detection;
Read static cache file If static write is turned on;
Application initialization filter plugin App_init;
Record application initialization time $GLOBALS [' _inittime ']
3. Project Pre-compilation
Load system routines configuration file convention.php;
Load the project configuration file config.php;
Load project public file common.php;
If it is debug mode, load the system debug configuration file debug.php;
If the Debug configuration file that defines the project is loaded into the debug.php;
Build the project compile cache file ~app.php;
4. URL Analysis Dispatcher
Check the current URL mode Url_model;
If a $_get variable exists, it is redirected based on the current URL pattern and settings;
conduct route definition detection;
Parse the URL information of the path_info into an array;
Combine the value obtained by the path_info with the $_get;
5. Get the module and operation name
Check the Var_module variable (including get and post), and if not defined, get the default module name;
Check component module;
Check the module camouflage;
Check the var_action variable (including get and post) and, if undefined, get the default action name;
Check operation chain;
Check the operation camouflage;
6. Exec Project execution
Auto_load_class Check if there is a public class to import;
Instantiate the action controller class for the current module;
If the action controller does not exist, check the empty module emptyaction;
Check the chain of operations if there is a chain of execution operations;
Check the predecessor Operation Method _before_ operation name;
Execute the Operation method of the module, dispatch to the action controller;
Perform the post-operation Method _after_ operation name;
Execute application end filter app_end;
If logging is turned on, the error log is written;
7. Perform the operation of the controller
materialized views Class View;
Obtain the current controller name;
Controller initialization _initialize;
If the operation method does not exist check the empty operation _empty;
Check the corresponding template file if the empty operation is not defined;
Call the model to get the data;
Render the view for output;
8. Call the model to get the data find
Instantiate the model class;
Model initialization of _initialize;
Determine the current model name and the corresponding data table;
Instantiate the database Operation object;
Data table field detection and caching;
Query the required data;
Judging whether the view model;
If the delay query returns the Resultiterator object;
Record optimistic lock when data object is removed;
Gets the text field data;
Get the associated data;
Automatic code conversion for data objects;
Record the current data object;
Returns the defined data format (array or Stdclass object)
9. Output view
Template variable assignment;
Detects if the layout is output;
Detection page output coding;
Cache initialization filter Ob_init;
Page cache open Ob_start;
Filtering performed after the cache is opened;
Template file name filter template_file;
Locate the template file for the current output;
Template variable filter template_var;
According to the different template engine processing;
If it is a PHP template engine, load the template file directly;
Use the built-in template engine to detect cache lifetime;
If the cache is invalid, recompile the template file;
Loading template cache files;
Get and empty the cache;
Output encoding conversion;
Output filter ob_content;
Open static write to static file;
If the output gets the view run time;
If it is display, render the template output information;
Turn on the page trace to display the page trace information;
Returns the template output information if it is a fetch;
ThinkPHP2.2 Framework execution Flowchart, thinkphp controller execution flow