Home > PHP Framework > ThinkPHP > body text

Does thinkphp5 support swoole Ctrip?

王林
Release: 2019-09-12 11:38:26
forward
3424 people have browsed it

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.

Does thinkphp5 support swoole Ctrip?

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.

Does thinkphp5 support swoole Ctrip?

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(
    [
        &#39;enable_static_handler&#39; => true,
        &#39;document_root&#39; => "/data/wwwroot/zhibo/public/static",
        &#39;worker_num&#39; => 5,//产生进程的个数
    ]
);
$http->on(&#39;WorkerStart&#39;,function ($ser,$worker_id){
    define(&#39;APP_PATH&#39;, __DIR__ . &#39;/../application/&#39;);
    require __DIR__ . &#39;/../thinkphp/base.php&#39;;
});
$http->on(&#39;request&#39;, 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(&#39;app&#39;, [APP_PATH])->run()->send();
    }catch (\Exception $e){

    }
    $res = ob_get_contents();
    ob_end_clean();
    $response->end($res);
    //$http->close();
});
$http->start();
Copy after login

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.

Does thinkphp5 support swoole Ctrip?

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:

Does thinkphp5 support swoole Ctrip?

Does thinkphp5 support swoole Ctrip?

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!

Related labels:
source:csdn.net
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template