Analyze ThinkPHP5 loading process
Installing ThinkPHP
How to install it, I won’t go into details. The official document-Installing ThinkPHP talks a lot Complete, you can download the zip package through Composer, Git or go directly to the ThinkPHP official website. The version I installed is 5.0.24
Test run
After downloading and installing, 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 figure below. Indicates that ThinkPHP5 has been installed successfully
In addition to the above method of address operation, we can also configure a virtual host through Apache or Nginx to access the project. Those who are interested can view it online Specific tutorial, 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 (public\index.php)
Open public\ index.php file, we can see that the original code of the entry file is as follows
1 2 3 4 5 |
|
The entry file code is very simple, just two lines of code, with the functions
1 2 |
|
In addition to the two above In addition to the function, we can also define our own constants in the entry file, such as adding a line of code define('PUBLIC_PATH', __DIR__ .'/../public'); to define the constants of the public directory and some preprocessing.
Load the framework boot file (thinkphp\start.php)
Similarly, after entering the thinkphp\start.php file, we can know that there is not much code
1 2 3 4 5 6 |
|
From this short two lines of code, we can see that there are two main left and right sides
1 2 |
|
The following two major points will introduce in detail what these two left and right sides do
Load the base file (thinkphp\base.php)
We continue to open the thinkphp\base.php file and find that this file is no longer like the first two files, with only two Line of code...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|
Looking carefully, I found that although the code has more than 60 lines, the function of the code is obvious. The main functions are the following six points
- Use
define('', '')
The function defines many system constants, plus two environment constants - Introduce the loader class (thinkphp\library\think\loader.php) for subsequent use
- Load the environment variable configuration file (the name of the environment variable configuration file is
.env
. This file does not necessarily exist. It is added as needed during the actual development process) - Calling
\think\Loader::register()
Register automatic loading mechanism- Register system automatic loading
Composer
Automatic loading support- Register namespace definition
- Load the class library mapping file, which exists in the
runtime
cache directoryclassmap.php
- Automatic loading
extend
Directory
- Call
\think\Error::register()
Register exception and error handling mechanism - Load custom configuration File (thinkphp\convention.php)
Execute the run method under the application (thinkphp\library\think\App.php)
For convenience, this Although the code of the run method is a bit long, I still choose to post the entire method. Don’t hit me.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
What exactly does this about 90 lines of code do? Based on the analysis of comments, the main ones are as follows: Step function
- The first step: Process the variable
$request
, ensure that it is valid and not null - The second step:
self::initCommon()
Call The initCommon() method in the current controller is responsible for initializing the application and returning configuration information-
Loader::addNamespace(self::$namespace, APP_PATH);
Register namespace -
self::init()
Call the init() method of this class to initialize the application- Load various configuration files
- Load behavior extension files
- Load public files
- Load language pack
- Apply debug mode related processing
- Load additional files through configuration items
extra_file_list
Value to load related files -
date_default_timezone_set($config['default_timezone']);
Set the system time zone - Call
Hook::listen('app_init ');
The behavior of monitoring the app_init tag
-
- Step 3: Determine whether to bind the module or controller
- Step 4: System language setting and loading
- The fifth step:
self::routeCheck($request, $config)
Load the routeCheck() method of the current controller for route detection- Route first Address configuration detection, first read the cache route, if it does not exist, then import the routing file configuration
- No routing configuration, directly parse the module/controller/operation
- Return module module information (module name, control Device name and operation method name)
- Step 6: Enable debugging mode and record routing and request information logs
- Step 7:
self:: exec($dispatch, $config)
Call the exec() method in the controller to execute the call distribution- Perform distribution processing according to the user request type, here is the module module type
- Call
self::module()
Execute the 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 output interface seen by the user
Recommended tutorials: "PHP" "ThinkPHP Tutorial"
The above is the detailed content of Analyze ThinkPHP5 loading process. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.
