Installation of ThinkPHP
I will not go into details about how to install it. The official document - Installation of ThinkPHP is very complete. You can download the zip package through Composer, Git or directly from the ThinkPHP official website. The version I installed is 5.0.24
Test run
Download and installation completed Finally, if the project download directory is in the project root directory of your local server, you can directly enter the address http://localhost/thinkphp5/public/
in the browser to enter the default welcome page of ThinkPHP5 , as shown in the picture below, this means that ThinkPHP5 has been installed successfully
In addition to running the address in this way above, we can also run it through Apache or Nginx Configure the virtual host to access the project. If you are interested, you can check the specific tutorial online, and then configure the virtual host for access.
Let’s get to the point, let’s analyze the execution process of ThinkPHP5 step by step...
Entry file (publicindex.php)
Openpublic\index.php
After the file, we can see that the original code of the entry file is as follows
//?[?應用入口文件?] //?定義應用目錄 define('APP_PATH',?__DIR__?.?'/../application/'); //?加載框架引導文件 require?__DIR__?.?'/../thinkphp/start.php';
The entry file code is very concise, just two lines of code, the functions are
-
define(' APP_PATH', __DIR__ . '/../application/');
Constant APP_PATH that defines the application directory -
require __DIR__ . '/../thinkphp/start.php';
Load framework boot file
In addition to the above two functions, we can also define our own constants in the entry file, such as adding a line of codedefine('PUBLIC_PATH' , __DIR__ .'/../public');
Define the constants of the public directory and some preprocessing, etc.
Load the framework boot file (thinkphpstart.php)
Similarly, enter thinkphp\start.php
After file, we can know that there are not many codes
namespace?think; //?ThinkPHP?引導文件 //?1.?加載基礎文件 require?__DIR__?.?'/base.php'; //?2.?執(zhí)行應用 App::run()->send();
From these two short lines of code, we can see that there are two main left and right
-
require __DIR__ . '/base.php';
Load base file -
App::run()->send();
Execute the application
The following two major points will introduce in detail what these two left and right have done
Load the basic file (thinkphpbase.php)
Let’s continue Open the thinkphp\base.php
file and find that this file no longer has only two lines of code like the previous two files...
define('THINK_VERSION',?'5.0.24'); define('THINK_START_TIME',?microtime(true)); define('THINK_START_MEM',?memory_get_usage()); define('EXT',?'.php'); define('DS',?DIRECTORY_SEPARATOR); defined('THINK_PATH')?or?define('THINK_PATH',?__DIR__?.?DS); define('LIB_PATH',?THINK_PATH?.?'library'?.?DS); define('CORE_PATH',?LIB_PATH?.?'think'?.?DS); define('TRAIT_PATH',?LIB_PATH?.?'traits'?.?DS); defined('APP_PATH')?or?define('APP_PATH',?dirname($_SERVER['SCRIPT_FILENAME'])?.?DS); defined('ROOT_PATH')?or?define('ROOT_PATH',?dirname(realpath(APP_PATH))?.?DS); defined('EXTEND_PATH')?or?define('EXTEND_PATH',?ROOT_PATH?.?'extend'?.?DS); defined('VENDOR_PATH')?or?define('VENDOR_PATH',?ROOT_PATH?.?'vendor'?.?DS); defined('RUNTIME_PATH')?or?define('RUNTIME_PATH',?ROOT_PATH?.?'runtime'?.?DS); defined('LOG_PATH')?or?define('LOG_PATH',?RUNTIME_PATH?.?'log'?.?DS); defined('CACHE_PATH')?or?define('CACHE_PATH',?RUNTIME_PATH?.?'cache'?.?DS); defined('TEMP_PATH')?or?define('TEMP_PATH',?RUNTIME_PATH?.?'temp'?.?DS); defined('CONF_PATH')?or?define('CONF_PATH',?APP_PATH);?//?配置文件目錄 defined('CONF_EXT')?or?define('CONF_EXT',?EXT);?//?配置文件后綴 defined('ENV_PREFIX')?or?define('ENV_PREFIX',?'PHP_');?//?環(huán)境變量的配置前綴 //?環(huán)境常量 define('IS_CLI',?PHP_SAPI?==?'cli'???true?:?false); define('IS_WIN',?strpos(PHP_OS,?'WIN')?!==?false); //?載入Loader類 require?CORE_PATH?.?'Loader.php'; //?加載環(huán)境變量配置文件 if?(is_file(ROOT_PATH?.?'.env'))?{ ????$env?=?parse_ini_file(ROOT_PATH?.?'.env',?true); ????foreach?($env?as?$key?=>?$val)?{ ????????$name?=?ENV_PREFIX?.?strtoupper($key); ????????if?(is_array($val))?{ ????????????foreach?($val?as?$k?=>?$v)?{ ????????????????$item?=?$name?.?'_'?.?strtoupper($k); ????????????????putenv("$item=$v"); ????????????} ????????}?else?{ ????????????putenv("$name=$val"); ????????} ????} } //?注冊自動加載 \think\Loader::register(); //?注冊錯誤和異常處理機制 \think\Error::register(); //?加載慣例配置文件 \think\Config::set(include?THINK_PATH?.?'convention'?.?EXT);
Looking carefully, I found that although the code has six More than ten lines, but the function of the code is obvious. The main functions include the following six points
- Use the
define('', '')
function to define many system constants. Plus two environment constants - Introduce the loader class (thinkphplibrarythinkloader.php) for subsequent use
- Load the environment variable configuration file (the environment variable configuration file is named
.env
, This file does not necessarily exist, it is added as needed during the actual development process) -
Call
\think\Loader::register()
Register automatic loading mechanism- Registration system automatic loading
-
Composer
Automatic loading support - Registration namespace definition
- Load class library mapping file, exists In the
runtime
cache directoryclassmap.php
- automatically load
extend
directory
##call - \think\Error::register()
Register exception and error handling mechanism
Load the convention configuration file (thinkphpconvention.php)
/** ?*?執(zhí)行應用程序 ?*?@access?public ?*?@param??Request?$request?請求對象 ?*?@return?Response ?*?@throws?Exception ?*/ public?static?function?run(Request?$request?=?null) { ????$request?=?is_null($request)???Request::instance()?:?$request; ????try?{ ????????$config?=?self::initCommon(); ????????//?模塊/控制器綁定 ????????if?(defined('BIND_MODULE'))?{ ????????????BIND_MODULE?&&?Route::bind(BIND_MODULE); ????????}?elseif?($config['auto_bind_module'])?{ ????????????//?入口自動綁定 ????????????$name?=?pathinfo($request->baseFile(),?PATHINFO_FILENAME); ????????????if?($name?&&?'index'?!=?$name?&&?is_dir(APP_PATH?.?$name))?{ ????????????????Route::bind($name); ????????????} ????????} ????????$request->filter($config['default_filter']); ????????//?默認語言 ????????Lang::range($config['default_lang']); ????????//?開啟多語言機制?檢測當前語言 ????????$config['lang_switch_on']?&&?Lang::detect(); ????????$request->langset(Lang::range()); ????????//?加載系統(tǒng)語言包 ????????Lang::load([ ????????????THINK_PATH?.?'lang'?.?DS?.?$request->langset()?.?EXT, ????????????APP_PATH?.?'lang'?.?DS?.?$request->langset()?.?EXT, ????????]); ????????//?監(jiān)聽?app_dispatch ????????Hook::listen('app_dispatch',?self::$dispatch); ????????//?獲取應用調度信息 ????????$dispatch?=?self::$dispatch; ????????//?未設置調度信息則進行?URL?路由檢測 ????????if?(empty($dispatch))?{ ????????????$dispatch?=?self::routeCheck($request,?$config); ????????} ????????//?記錄當前調度信息 ????????$request->dispatch($dispatch); ????????//?記錄路由和請求信息 ????????if?(self::$debug)?{ ????????????Log::record('[?ROUTE?]?'?.?var_export($dispatch,?true),?'info'); ????????????Log::record('[?HEADER?]?'?.?var_export($request->header(),?true),?'info'); ????????????Log::record('[?PARAM?]?'?.?var_export($request->param(),?true),?'info'); ????????} ????????//?監(jiān)聽?app_begin ????????Hook::listen('app_begin',?$dispatch); ????????//?請求緩存檢查 ????????$request->cache( ????????????$config['request_cache'], ????????????$config['request_cache_expire'], ????????????$config['request_cache_except'] ????????); ????????$data?=?self::exec($dispatch,?$config); ????}?catch?(HttpResponseException?$exception)?{ ????????$data?=?$exception->getResponse(); ????} ????//?清空類的實例化 ????Loader::clearInstance(); ????//?輸出數據到客戶端 ????if?($data?instanceof?Response)?{ ????????$response?=?$data; ????}?elseif?(!is_null($data))?{ ????????//?默認自動識別響應輸出類型 ????????$type?=?$request->isAjax()?? ????????Config::get('default_ajax_return')?: ????????Config::get('default_return_type'); ????????$response?=?Response::create($data,?$type); ????}?else?{ ????????$response?=?Response::create(); ????} ????//?監(jiān)聽?app_end ????Hook::listen('app_end',?$response); ????return?$response; }This is about 90 lines of code , what exactly is done, combined with the annotation analysis, the main functions are the following steps
- The first step: Process the variable
- $request
to ensure that it is valid and not null
- Second step:
self::initCommon()
Call the initCommon() method in the current controller, which is responsible for initializing the application and returning configuration information
- Loader::addNamespace(self::$namespace, APP_PATH);
Register namespace
-
self::init()
Call init of this class () method initializes the application
- Load various configuration files
- Load behavior extension files
- Load public files
- Load language pack
- Apply debug mode related processing
- Load additional files and load related files through the value of the configuration item
extra_file_list
-
date_default_timezone_set($config['default_timezone'] );
Set the system time zone - Call
Hook::listen('app_init');
The behavior of listening to the app_init tag
##Third Step: Determine whether to bind the module or controller - Loader::addNamespace(self::$namespace, APP_PATH);
- Step 4: System language setting and loading
- Step 5:
self::routeCheck($request , $config)
Load the routeCheck() method of the current controller for route detection
- First perform routing address configuration detection, first read the cached route, and then import the routing file configuration if it does not exist
- No routing configuration, directly parse module/controller/operation
- Return module module information (module name, controller name and operation method name)
Step 6: Enable debugging mode and record the log of routing and request information - Step 7:
self::exec($dispatch, $config)
Call in the controller The exec() method executes the call distribution
- Distribution processing is performed according to the user request type. Here is the module module type
- Call
- self::module()
Execution module , perform module deployment and initialization, obtain and set the current controller name and operation name
Step 8: Clear the instantiation of the class, and output data in the corresponding format to the client, that is, the user The output interface you see