当前位置 知且行 php 正文 下一篇:

swoole与thinkphp5结合

源码参考

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

https://segmentfault.com/a/1190000014572291

安装thinkphp

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

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

新建 tp5/server/http_server.php

  1. <?php
  2. $http = new swoole_http_server("0.0.0.0", 8811);
  3. $http->set(
  4. [
  5. 'enable_static_handler' => true,
  6. 'document_root' => "/swoole/project/thinkphp/public/static",
  7. ]
  8. );
  9. $http->on('request', function($request, $response) {
  10. $response->cookie("singwa", "xsssss", time() + 1800);
  11. $response->end("sss". json_encode($request->get));
  12. });
  13. $http->start();

终端启动:

  1. server [master] php http_server.php

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

swoole 与 thinkphp5 结合

/swoole/tp5/server/http_server.php

  1. <?php
  2. $http = new swoole_http_server("0.0.0.0", 8811);
  3. $http->set(
  4. [
  5. 'enable_static_handler' => true,
  6. 'document_root' => __DIR__ . "/../public/static",
  7. ]
  8. );
  9. $http->on('WorkerStart', function(swoole_server $server, $worker_id) {
  10. // 定义应用目录
  11. define('APP_PATH', __DIR__ . '/../application/');
  12. // 加载框架里面的文件
  13. require __DIR__ . '/../thinkphp/base.php';
  14. });
  15. $http->on('request', function($request, $response) {
  16. $response->header("Content-Type", "text/html; charset=utf-8");
  17. // 启用ob缓存获取内容
  18. ob_start();
  19. think\App::run()->send();
  20. $res = ob_get_contents();
  21. ob_end_clean();
  22. $response->end($res); // 将结果输出到浏览器
  23. });
  24. $http->start();

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

  1. <?php
  2. namespace app\index\controller;
  3. class Index
  4. {
  5. public function index()
  6. {
  7. return 'thinkphp与swoole勾搭上了...';
  8. }
  9. }

重启服务 php http_server.php

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

tp5

全局变量污染

/swoole/tp5/server/http_server.php

  1. <?php
  2. $http = new swoole_http_server("0.0.0.0", 8811);
  3. $http->set(
  4. [
  5. 'enable_static_handler' => true,
  6. 'document_root' => __DIR__ . "/../public/static",
  7. ]
  8. );
  9. $http->on('WorkerStart', function(swoole_server $server, $worker_id) {
  10. // 定义应用目录
  11. define('APP_PATH', __DIR__ . '/../application/');
  12. // 加载框架里面的文件
  13. require __DIR__ . '/../thinkphp/base.php';
  14. });
  15. $http->on('request', function($request, $response) {
  16. $response->header("Content-Type", "text/html; charset=utf-8");
  17. $_SERVER = [];
  18. if(isset($request->server)) {
  19. foreach($request->server as $k => $v) {
  20. $_SERVER[strtoupper($k)] = $v;
  21. }
  22. }
  23. if(isset($request->header)) {
  24. foreach($request->header as $k => $v) {
  25. $_SERVER[strtoupper($k)] = $v;
  26. }
  27. }
  28. $_GET = [];
  29. if(isset($request->get)) {
  30. foreach($request->get as $k => $v) {
  31. $_GET[$k] = $v;
  32. }
  33. }
  34. $_POST = [];
  35. if(isset($request->post)) {
  36. foreach($request->post as $k => $v) {
  37. $_POST[$k] = $v;
  38. }
  39. }
  40. // 启用ob缓存获取内容
  41. ob_start();
  42. think\App::run()->send();
  43. $res = ob_get_contents();
  44. ob_end_clean();
  45. $response->end($res); // 将结果输出到浏览器
  46. });
  47. $http->start();

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

  1. <?php
  2. namespace app\index\controller;
  3. class Index
  4. {
  5. public function index()
  6. {
  7. var_dump($_GET);
  8. }
  9. }

路由变更

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

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

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

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

追溯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

  1. public function pathinfo()
  2. {
  3. echo 'is_cli-' . IS_CLI . '<br />';
  4. // ...
  5. }

修改源码

  1. public function pathinfo()
  2. {
  3. if (isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] != '/') {
  4. return ltrim($_SERVER['PATH_INFO'], '/');
  5. }
  6. // ...
  7. }

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

其他

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

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

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

关于我

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

微信扫一扫关注我

返回顶部