search
HomePHP FrameworkThinkPHPDoes thinkphp5 support swoole Ctrip?

Does thinkphp5 support swoole Ctrip?

Sep 12, 2019 am 11:38 AM
swoolethinkphp5support

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();

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!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

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

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.