Does thinkphp5 support swoole Ctrip?
First of all, let’s understand the operating mechanism of ThinkPHP.
TP5 operating mechanism:
Any request will go through the tp5 entry file, load the framework configuration file, start the process, and then process the request.
In the entry file of index.php, you can see that it first defines the APP_PATH constant, and then introduces the startup file start.php of the framework, then we Let’s take a look at what the start.php file does.
Here, it first loads the base file base.php, then starts the framework run, and then starts processing the request.
Conventional nginx, Apache server, every time a request comes to thinkphp, the static variables will be cleared and the configuration file will be reloaded. However, the server made by swoole is a resident process. After starting the service, multiple processes will be generated to handle the request. We want to make it selectively load configuration.
Swoole to be the http server
<?php $http = new swoole_http_server("0.0.0.0", 8888); $http->set( [ 'enable_static_handler' => true, 'document_root' => "/data/wwwroot/zhibo/public/static", 'worker_num' => 5,//产生进程的个数 ] ); $http->on('WorkerStart',function ($ser,$worker_id){ define('APP_PATH', __DIR__ . '/../application/'); require __DIR__ . '/../thinkphp/base.php'; }); $http->on('request', function($request, $response) use($http) { if(isset($request->header)){ foreach ($request->header as $k=>$v){ $_SERVER[strtoupper($k)] = $v; } } if(isset($request->server)){ foreach ($request->server as $k=>$v){ $_HEADER[strtoupper($k)] = $v; } } $_GET = []; if(isset($request->get)){ foreach ($request->get as $k=>$v){ $_GET[$k] = $v; } } $_POST = []; if(isset($request->post)){ foreach ($request->post as $k=>$v){ $_POST[$k] = $v; } } // 执行应用并响应 //开启缓存 ob_start(); try{ think\Container::get('app', [APP_PATH])->run()->send(); }catch (\Exception $e){ } $res = ob_get_contents(); ob_end_clean(); $response->end($res); //$http->close(); }); $http->start();
Code Description:
(1)$http ->onWorkerStart: When starting the process, the thinkphp framework file, base.php, is loaded. However, at this time, it cannot be run. It waits for the request to come before running.
(2) $http->onrequest: When receiving a request from the client, convert swoole header information, server information, get data, post data and other messages into regular $_SERVER, $_GET and other information, can be adapted to tp5.
(3) Finally start the run. At this time, you need to load the information obtained by the run into the cache, and then return it to the client through send().
Swoole adapts to thinkphp5
Because swoole is a resident process, the $_POST and $_GET requests of the previous request will not be destroyed, because this process is not killed. At this time, you need to leave $_GET and $_POST empty when receiving the request.
The swoole routing mechanism will always obtain whether there is such a request from the cache. If there is, no new one will be loaded. Therefore, swoole is resident in memory and will find that the first url is always requested. Unless restarting the swoole server.
In the thinkphp framework, modify the Request file and remove the empty judgment of $this->path in the two methods (pathinfo, path), so that each This URL is parsed for every request.
Verification result:
The above content is for reference only!
If you want to know more related content, please visit the php Chinese website: thinkphp tutorial
The above is the detailed content of Does thinkphp5 support swoole Ctrip?. 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











Using Swoole coroutines in Laravel can process a large number of requests concurrently. The advantages include: Concurrent processing: allows multiple requests to be processed at the same time. High performance: Based on the Linux epoll event mechanism, it processes requests efficiently. Low resource consumption: requires fewer server resources. Easy to integrate: Seamless integration with Laravel framework, simple to use.

When using Windows Shello, a supported camera cannot be found. The common reasons are that the camera used does not support face recognition and the camera driver is not installed correctly. So let's take a look at how to set it up. Windowshello cannot find a supported camera tutorial: Reason 1: The camera driver is not installed correctly 1. Generally speaking, the Win10 system can automatically install drivers for most cameras, as follows, there will be a notification after plugging in the camera; 2. At this time, we open the device Check the manager to see if the camera driver is installed. If not, you need to do it manually. WIN+X, then select Device Manager; 3. In the Device Manager window, expand the camera option, and the camera driver model will be displayed.

Pros and cons of open source software: Understanding the pros and cons of open source projects requires specific code examples In today’s digital age, open source software is getting more and more attention and respect. As a software development model based on the spirit of cooperation and sharing, open source software is widely used in different fields. However, despite the many advantages of open source software, there are also some challenges and limitations. This article will delve into the pros and cons of open source software and demonstrate the pros and cons of open source projects through specific code examples. 1. Advantages of open source software 1.1 Openness and transparency Open source software

ASUS tufz790plus supports memory frequency. ASUS TUFZ790-PLUS motherboard is a high-performance motherboard that supports dual-channel DDR4 memory and supports up to 64GB of memory. Its memory frequency is very powerful, up to 4800MHz. Specific supported memory frequencies include 2133MHz, 2400MHz, 2666MHz, 2800MHz, 3000MHz, 3200MHz, 3600MHz, 3733MHz, 3866MHz, 4000MHz, 4133MHz, 4266MHz, 4400MHz, 4533MHz, 4600MHz, 4733MHz and 4800MHz. Whether it is daily use or high performance needs

Performance comparison: Throughput: Swoole has higher throughput thanks to its coroutine mechanism. Latency: Swoole's coroutine context switching has lower overhead and smaller latency. Memory consumption: Swoole's coroutines occupy less memory. Ease of use: Swoole provides an easier-to-use concurrent programming API.

To restart the Swoole service, follow these steps: Check the service status and get the PID. Use "kill -15 PID" to stop the service. Restart the service using the same command that was used to start the service.

Does PyCharm Community Edition support enough plugins? Need specific code examples As the Python language becomes more and more widely used in the field of software development, PyCharm, as a professional Python integrated development environment (IDE), is favored by developers. PyCharm is divided into two versions: professional version and community version. The community version is provided for free, but its plug-in support is limited compared to the professional version. So the question is, does PyCharm Community Edition support enough plug-ins? This article will use specific code examples to

Swoole and Workerman are both high-performance PHP server frameworks. Known for its asynchronous processing, excellent performance, and scalability, Swoole is suitable for projects that need to handle a large number of concurrent requests and high throughput. Workerman offers the flexibility of both asynchronous and synchronous modes, with an intuitive API that is better suited for ease of use and projects that handle lower concurrency volumes.
