Home> PHP Framework> ThinkPHP> body text

Analyze the loading process of TP5 framework from entry to output interface

藏色散人
Release: 2021-09-16 15:12:28
forward
2677 people have browsed it

thinkphp frameworkThe tutorial column will introduce and analyze the loading process of the ThinkPHP5 framework from the entrance to the output interface. I hope it will be helpful to friends in need!

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 addresshttp://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.phpAfter 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';
Copy after login

The entry file code is very concise, just two lines of code, the functions are

  1. define(' APP_PATH', __DIR__ . '/../application/');Constant APP_PATH that defines the application directory
  2. 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, enterthinkphp\start.phpAfter file, we can know that there are not many codes

namespace think; // ThinkPHP 引导文件 // 1. 加载基础文件 require __DIR__ . '/base.php'; // 2. 执行应用 App::run()->send();
Copy after login

From these two short lines of code, we can see that there are two main left and right

  1. require __DIR__ . '/base.php';Load base file
  2. 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 thethinkphp\base.phpfile 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);
Copy after login

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

  1. Use thedefine('', '')function to define many system constants. Plus two environment constants
  2. Introduce the loader class (thinkphplibrarythinkloader.php) for subsequent use
  3. 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)
  4. Call\think\Loader::register()Register automatic loading mechanism

    • Registration system automatic loading
    • ComposerAutomatic loading support
    • Registration namespace definition
    • Load class library mapping file, exists In theruntimecache directoryclassmap.php
    • automatically loadextenddirectory
  5. ##call
  6. \think\Error::register()Register exception and error handling mechanism
  7. Load the convention configuration file (thinkphpconvention.php)
Execute the application (thinkphplibrarythinkApp.php )

For convenience, although the code of this run method is a bit long, I still choose to post the entire method, don’t hit me

/** * 执行应用程序 * @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; }
Copy after login
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
  • $requestto 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 itemextra_file_list
    • date_default_timezone_set($config['default_timezone'] );Set the system time zone
    • CallHook::listen('app_init');The behavior of listening to the app_init tag
  • ##Third Step: Determine whether to bind the module or controller
  • 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
Summary

This article roughly analyzes the basic execution process of ThinkPHP5. If there are any things that are not in place, there is no need to tell me, because I don’t know how. I just made it up, that's it. But if it's wrong, please point it out and I will definitely correct it. It's that honest. By the way, if you think it is helpful to you, please give it a like and leave. Thank you!

The above is the detailed content of Analyze the loading process of TP5 framework from entry to output interface. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!