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. 执行应用 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_'); // 环境变量的配置前缀 // 环境常量 define('IS_CLI', PHP_SAPI == 'cli' ? true : false); define('IS_WIN', strpos(PHP_OS, 'WIN') !== false); // 载入Loader类 require CORE_PATH . 'Loader.php'; // 加载环境变量配置文件 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)
/** * 执行应用程序 * @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()); // 加载系统语言包 Lang::load([ THINK_PATH . 'lang' . DS . $request->langset() . EXT, APP_PATH . 'lang' . DS . $request->langset() . EXT, ]); // 监听 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'); } // 监听 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(); } // 监听 app_end Hook::listen('app_end', $response); return $response; }
- 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