当前位置 知且行 php swoole与thinkphp5结合 下一篇:

swoole与thinkphp5结合

源码参考

https://github.com/guaosi/swoole-live-demo

https://segmentfault.com/a/1190000014572291

安装thinkphp

composer create-project topthink/think=5.0.* tp5  --prefer-dist

将静态文件置于:thinkphp/public/static

新建 tp5/server/http_server.php

<?php
$http = new swoole_http_server("0.0.0.0", 8811);

$http->set(
    [
        'enable_static_handler' => true,
        'document_root' => "/swoole/project/thinkphp/public/static",
    ]
);
$http->on('request', function($request, $response) {
    $response->cookie("singwa", "xsssss", time() + 1800);
    $response->end("sss". json_encode($request->get));
});

$http->start();

终端启动:

☁  server [master] ⚡ php http_server.php

浏览器访问:http://127.0.0.1:8811/live/detail.html

swoole 与 thinkphp5 结合

/swoole/tp5/server/http_server.php

<?php
$http = new swoole_http_server("0.0.0.0", 8811);

$http->set(
    [
        'enable_static_handler' => true,
        'document_root' => __DIR__ . "/../public/static",
    ]
);
$http->on('WorkerStart', function(swoole_server $server,  $worker_id) {
    // 定义应用目录
    define('APP_PATH', __DIR__ . '/../application/');
    // 加载框架里面的文件
    require __DIR__ . '/../thinkphp/base.php';
});

$http->on('request', function($request, $response) {
    $response->header("Content-Type", "text/html; charset=utf-8");
    
    // 启用ob缓存获取内容
    ob_start();
    think\App::run()->send();
    $res = ob_get_contents();
    ob_end_clean();

    $response->end($res); // 将结果输出到浏览器
});

$http->start();

/swoole/tp5/application/index/controller/Index.php

<?php
namespace app\index\controller;

class Index
{
    public function index()
    {
        return 'thinkphp与swoole勾搭上了...';
    }
}

重启服务 php http_server.php

查看效果http://127.0.0.1:8811/

tp5

全局变量污染

/swoole/tp5/server/http_server.php

<?php
$http = new swoole_http_server("0.0.0.0", 8811);

$http->set(
    [
        'enable_static_handler' => true,
        'document_root' => __DIR__ . "/../public/static",
    ]
);
$http->on('WorkerStart', function(swoole_server $server,  $worker_id) {
    // 定义应用目录
    define('APP_PATH', __DIR__ . '/../application/');
    // 加载框架里面的文件
    require __DIR__ . '/../thinkphp/base.php';
});

$http->on('request', function($request, $response) {
    $response->header("Content-Type", "text/html; charset=utf-8");

    $_SERVER  =  [];
    if(isset($request->server)) {
        foreach($request->server as $k => $v) {
            $_SERVER[strtoupper($k)] = $v;
        }
    }
    if(isset($request->header)) {
        foreach($request->header as $k => $v) {
            $_SERVER[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缓存获取内容
    ob_start();
    think\App::run()->send();
    $res = ob_get_contents();
    ob_end_clean();

    $response->end($res); // 将结果输出到浏览器
});

$http->start();

/swoole/tp5/application/index/controller/Index.php

<?php
namespace app\index\controller;

class Index
{
    public function index()
    {
        var_dump($_GET);
    }
}

路由变更

url用普通模式:http://127.0.0.1:8811/index.php?s=index/index/test

$path 变量由于常驻内存,在第一次写入后,第二次不会再更新

/swoole/tp5/thinkphp/library/think/App.php

    public static function routeCheck($request, array $config)
    {
        $path   = $request->path();
        echo 'path-' . $path . '<br />'; // 变更url,可看到path并没有改变
        $depr   = $config['pathinfo_depr'];
        // ...
    }

追溯path的获取方式

/swoole/tp5/thinkphp/library/think/Request.php

注释path的判断1

注释path的判断2

url 使用pathinfo模式

http://127.0.0.1:8811/index/index/index

获取不到path

因为是用cli模式

/swoole/tp5/thinkphp/library/think/Request.php

    public function pathinfo()
    {
        echo 'is_cli-' . IS_CLI . '<br />';
        // ...
    }

修改源码

    public function pathinfo()
    {
        if (isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] != '/') {
            return ltrim($_SERVER['PATH_INFO'], '/');
        }
        // ...
    }

访问:http://127.0.0.1:8811/index/index/index 与 http://127.0.0.1:8811/index/index/test 均可实现

其他

用tp的get也需要改源码,直接用原生的$_GET

也可以将文件的引用移到request中,这样相当于每次响应请求时都会重新加载框架文件,性能较差

转载必须注明出处:https://www.zhiqiexing.com/54.html

关于我

我希望能成为一个认真、有趣、创造更多价值的人
关注微信
微信扫一扫关注我

微信扫一扫关注我

返回顶部