Tp5.1+版本与Swoole结合---学习笔记(5)

  • tp框架5.1版本
http://www.thinkphp.cn/down.html
  • 使用Swoole

$http = new swoole_http_server("0.0.0.0", 9501);
$http->set([
  'enable_static_handler'=>true,
  'document_root' => '/home/wwwroot/default/swool/demo/tp5
  /public/static',
  //设置终端执行保存的静态路径
  'worker_num' => 4,
]);
$http->on('WorkerStart', function ($http, $worker_id){
   require __DIR__ . '/../thinkphp/base.php';
});
//use($http)
$http->on('request', function ($request, $response) 
use($http) {
    // if (!empty($_SERVER)) {
    //   unset($_SERVER);
    // }
    // $_SERVER= [];
    if(isset($request->server)){
        foreach ($request->server as $key => $value) {
            $_SERVER[strtoupper($key)] = $value;
        }
    }
    if(isset($request->header)){
        foreach ($request->header as $key => $value) {
            $_SERVER[strtoupper($key)] = $value;
        }
    }
    // if (!empty($_GET)) {
    //   unset($_GET);
    // }
    // $_GET=[];
    if(isset($request->get)){
        foreach ($request->get as $key => $value) {
            $_GET[strtoupper($key)] = $value;
        }
    }
    // if (!empty($_POST)) {
    //   unset($_POST);
    // }
    // $_POST =[];
    if(isset($request->post)){
        foreach ($request->post as $key => $value) {
            $_POST[strtoupper($key)] = $value;
        }
    }
    ob_start();
    try {
      think\Container::get('app')->run()->send();
    } catch (\Exception $e) {

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

$http->start();
  • 在Worker进程/Task进程启动时发生,这里创建的对象可以在进程生命周期内使用
    在onWorkerStart中加载框架的核心文

Tp5.1+版本与Swoole结合---学习笔记(5)_第1张图片


  • 修改ThinkPHP5框架Request.php源码位置:
    /thinkphp/library/think/Request.php

function path() { }
//注销判断,不再复用类成员变量$this->path
function pathinfo() { }
//注销判断,不再复用类成员变量$this->pathinfo
使其支持pathinfo路由,添加如下代码在function pathinfo() { }中
if (isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] != '/') 
{
     return ltrim($_SERVER['PATH_INFO'], '/');
}

你可能感兴趣的:(Swool学习笔记)