Swoole 支持ThinkPHP5.1 非think-swoole

Swoole 支持ThinkPHP5.1 非think-swoole

  • 建立server端文件
  • 修改ThinkPHP源码

git下来ThinkPHP5.1 然后进行修改

建立server端文件

在项目根目录中建立 /server/server.php


/**
 * Created by PhpStorm.
 * User: dom
 * Date: 19-2-23
 * Time: 下午4:10
 */

$http = new swoole_http_server("0.0.0.0", 9906);

$http->set([
    'enable_static_handler' => true, // 开启静态资源存在优先访问
    'document_root' => '/www/swoole_tp5/public/static', // 静态资源目录
    'worker_num' => 5,
    'log_level' => SWOOLE_LOG_ERROR, // 日志等级 关闭开启debug
    'trace_flags' => SWOOLE_TRACE_SERVER, // 日志等级 关闭开启debug
]);

// 增加生命周期 进程启动时发生
$http->on('WorkerStart', function (swoole_server $server, $workerId) {
    // 加载基础文件
    require __DIR__ . '/../thinkphp/base.php';
});

$http->on('request', function ($request, $response) use ($http) {
	// 重写 server
    if (isset($request->server)) {
        foreach ($request->server as $k => $v) {
            $_SERVER[strtoupper($k)] = $v;
        }
    }

	// 重写 header
    if (isset($request->header)) {
        foreach ($request->header as $k => $v) {
            $_SERVER[strtoupper($k)] = $v;
        }
    }

	// 重写 get
    $_GET = [];
    if (isset($request->get)) {
        foreach ($request->get as $k => $v) {
            $_GET[$k] = $v;
        }
    }

	// 重写 post
    $_POST = [];
    if (isset($request->post)) {
        foreach ($request->post as $k => $v) {
            $_POST[$k] = $v;
        }
    }

    ob_start();
    // 执行应用并响应
    try {
        \think\Container::get('app')->run()->send();
    } catch (Exception $e) {

    }

    $res = ob_get_contents();
    ob_end_clean();

    $response->end($res);

});

$http->start();

如果你现在启动这个服务的话,你会发现以下的问题:
http://127.0.0.1:9907 打开之后 修改访问地址 如:http://127.0.0.1:9907/aaaa/dddd的变化始终不变

修改ThinkPHP源码

/thinkphp/library/think/Request.php
修改 public function pathinfo() 这个方法
注释 if (is_null($this->pathinfo)) {}

修改 public function path() 这个方法
注释 if (is_null($this->path)) {}

之后用 http://127.0.0.1:9907/?s=index/index 这种形式就可以解决以上问题

你可能感兴趣的:(工具使用,原创)