This article mainly introduces the thinkPHP5.0 framework application request life cycle, and analyzes in detail the various execution processes involved in the thinkPHP5.0 framework application request life cycle. Friends in need can refer to the following
The examples in this article describe the thinkPHP5.0 framework application request life cycle. Share it with everyone for your reference, the details are as follows:
In this article, we will give a general introduction to the life cycle of application requests in ThinkPHP5.0, so that developers can understand the entire execution process.
1. Entry file
Requests initiated by users will go through the application's entry file, usually the public/index.php file. Of course, you can also change or add new entry files.
Usually the code of the entry file is relatively simple. A common entry file code is as follows:
// 应用入口文件 // 定义项目路径 define('APP_PATH', __DIR__ . '/../application/'); // 加载框架引导文件 require __DIR__ . '/../thinkphp/start.php';
General entry files have defined some constants as Mainly, please refer to the following content or appendix for supported constants.
Generally, we do not recommend adding too much code to the application entry file, especially code related to business logic.
2. Boot file
The next step is the boot file of the execution framework. The start.php file is the default boot file of the system. In the boot file, the following operations will be performed in sequence:
① Load system constant definitions;
② Load environment variable definition files;
③ Register the automatic loading mechanism;
④ Register Error and exception handling mechanism;
⑤ Load the conventional configuration file;
⑥ Execute the application;
If the default boot file is changed in your application entry file, the above execution process Changes may follow.
3. Register for automatic loading
The system will call the Loader::register() method to register for automatic loading. After this step is completed, all class libraries that comply with the specification ( Including third-party class libraries that Composer depends on loading) will be loaded automatically.
The automatic loading of the system consists of two parts:
① The automatic loading method of the registration system\think\Loader::autoload
② The registration system namespace definition
③ Load the class library mapping file (if it exists)
④ If there is a Composer installation, register Composer for automatic loading
⑤ Register the Extend extension directory
Automatic loading detection sequence of a class library For:
① Whether to define class library mapping;
② PSR-4 automatic loading detection;
③ PSR-0 automatic loading detection;
Yes See, the way to define class library mapping is the most efficient.
4. Register error and exception mechanism
Execute Error::register() to register error and exception handling mechanism.
consists of three parts:
① Application shutdown method: think\Error::appShutdown<br>
② Error handling method: think\Error:: appError<br>
③ Exception handling method: think\Error::appException
Registering the application shutdown method is to facilitate interception of some system errors.
During the entire application request life cycle, if an exception or serious error is thrown, it will cause the application to terminate early and output exception and error information in response.
5. Application initialization
The first step to execute an application is to initialize the application, including:
Load the application (public ) configuration;
Load application state configuration;
Load alias definition;
Load behavior definition;
Load public (function) file;
Load extended configuration file (defined by extra_config_list);
Load the extension function file (defined by extra_file_list);
Set the default time zone;
Load the system language package;
6. URL access detection
After the application initialization is completed, URL access detection will be performed, including PATH_INFO detection and URL suffix detection.
5.0 URL access must be a URL address in PATH_INFO mode (including compatible mode), for example:
http://serverName/index.php/index/index/hello/ val/value
So, if your environment can only support normal URL parameter access, you must use
http://serverName/index.php? s=/index/index/hello&val=value
If you access the entry file from the command line, use
$php index.php index/index/hello /val/value...
Can continue only after obtaining the normal $_SERVER['PATH_INFO']
parameters.
7. Route detection
If the url_route_on parameter is enabled, URL route detection will be performed first.
If a matching route is detected, the corresponding URL schedule will be registered according to the defined routing address.
5.0’s routing address supports the following methods:
Route to module/controller/operation;
Route to external redirection address;
Route to controller method ;
Routing to closure function;
Routing to class method;
Routing address may be affected by domain name binding.
If routing is turned off or routing detection is invalid, the default module/controller/operation analysis and identification will be performed.
如果在应用初始化的时候指定了应用调度方式,那么路由检测是可选的。
可以使用 \think\App::dispatch()
进行应用调度。
8、分发请求
在完成了URL检测和路由检测之后,路由器会分发请求到对应的路由地址,这也是应用请求的生命周期中最重要的一个环节。
在这一步骤中,完成应用的业务逻辑及数据返回。
建议统一使用return返回数据,而不是echo输出,如非必要,请不要执行exit中断。
直接echo输出的数据将无法进行自动转换响应输出的便利。
下面是系统支持的分发请求机制,可以根据情况选择:
模块/控制器/操作
这是默认的分发请求机制,系统会根据URL或者路由地址来判断当前请求的模块、控制器和操作名,并自动调用相应的访问控制器类,执行操作对应的方法。
该机制下面,首先会判断当前模块,并进行模块的初始化操作(和应用的初始化操作类似),模块的配置参数会覆盖应用的尚未生效的配置参数。
支持模块映射、URL参数绑定到方法,以及操作绑定到类等一些功能。
控制器方法
和前一种方式类似,只是无需判断模块、控制器和操作,直接分发请求到一个指定的控制器类的方法,因此没有进行模块的初始化操作。
外部重定向
可以直接分发请求到一个外部的重定向地址,支持指定重定向代码,默认为301重定向。
闭包函数
路由地址定义的时候可以直接采用闭包函数,完成一些相对简单的逻辑操作和输出。
类的方法
除了以上方式外,还支持分发请求到类的方法,包括:
静态方法:
'blog/:id'=>'\org\util\Blog::read'
类的方法:
'blog/:id'=>'\app\index\controller\Blog@read'
9、响应输出
控制器的所有操作方法都是return返回而不是直接输出,系统会调用Response::send
方法将最终的应用返回的数据输出到页面或者客户端,并自动转换成default_return_type
参数配置的格式。所以,应用执行的数据输出只需要返回一个正常的PHP数据即可。
10、应用结束
事实上,在应用的数据响应输出之后,应用并没真正的结束,系统会在应用输出或者中断后进行日志保存写入操作。
系统的日志包括用户调试输出的和系统自动生成的日志,统一会在应用结束的时候进行写入操作。
而日志的写入操作受日志初始化的影响。
相关推荐:
thinkphp框架里用linux的crontab写php的定时脚本
The above is the detailed content of thinkPHP5.0 framework application request life cycle analysis. For more information, please follow other related articles on the PHP Chinese website!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools