Home > Article > PHP Framework > 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!