TP5.1配置swoole——最全方法最实用
此方法仅用于运行此命令时调试 php think swoole:server -p 80
此方法仅用于运行此命令时调试 php think swoole:server -p 80
此方法仅用于运行此命令时调试 php think swoole:server -p 80
第一步:下载tp swoole插件 注意:TP版本必须大于5.1才支持
composer require topthink/think-swoole=2.0.*
第二步:修改config/swoole.php
第三步:config/swoole_server.php 替换原来的 'onRequest'方法
'onRequest' => function ($request, $response) { $response->header("Content-Type", "text/html;charset=utf8"); $_GET = []; if (isset($request->get)) { $_GET = $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; } } $_SERVER = []; if (isset($request->header)) { foreach ($request->header as $k => $v) { $k = strtoupper($k); $_SERVER[$k] = $v; } } if (isset($request->server)) { foreach ($request->server as $k => $v) { $k = strtoupper($k); $_SERVER[$k] = $v; } } $_COOKIE = []; if (isset($request->cookie)) { foreach ($request->cookie as $k => $v) { $_COOKIE[$k] = $v; } } ob_start(); ob_clean(); // 执行应用并响应 try { // 2. 执行应用 Container::get('app')->run()->send(); } catch (Exception $e) { $json = [ "code" => $e->getCode() ? $e->getCode() : 500, "msg" => $e->getMessage() ]; echo json_encode($json, JSON_UNESCAPED_UNICODE); } $res = ob_get_contents(); // ob_end_clean(); $response->end($res); // 执行应用并响应 // dump($request); // dump($response); // $response->end("<h1>Hello Swoole. #" . rand(1000, 9999) . "</h1>"); },
第四步:修改 thinkphp\library\think\Request.php 把默认的 path 方法替换为:
public function path() { if (is_null($this->path) || isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] != '/') { $suffix = $this->config['url_html_suffix']; $pathinfo = $this->pathinfo(); if (false === $suffix) { // 禁止伪静态访问 $this->path = $pathinfo; } elseif ($suffix) { // 去除正常的URL后缀 $this->path = preg_replace('/\.(' . ltrim($suffix, '.') . ')$/i', '', $pathinfo); } else { // 允许任何后缀访问 $this->path = preg_replace('/\.' . $this->ext() . '$/i', '', $pathinfo); } } return $this->path; }
第五步:修改 thinkphp\library\think\Request.php 把默认的 pathinfo 方法替换为:
public function pathinfo() { if (isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] != '/') { return ltrim($_SERVER['PATH_INFO'], '/'); } if (is_null($this->pathinfo)) { if (isset($_GET[$this->config['var_pathinfo']])) { // 判断URL里面是否有兼容模式参数 $pathinfo = $_GET[$this->config['var_pathinfo']]; unset($_GET[$this->config['var_pathinfo']]); unset($this->get[$this->config['var_pathinfo']]); } elseif ($this->isCli()) { // CLI模式下 index.php module/controller/action/params/... $pathinfo = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : ''; } elseif ('cli-server' == PHP_SAPI) { $pathinfo = strpos($this->server('REQUEST_URI'), '?') ? strstr($this->server('REQUEST_URI'), '?', true) : $this->server('REQUEST_URI'); } elseif ($this->server('PATH_INFO')) { $pathinfo = $this->server('PATH_INFO'); } // 分析PATHINFO信息 if (!isset($pathinfo)) { foreach ($this->config['pathinfo_fetch'] as $type) { if ($this->server($type)) { $pathinfo = (0 === strpos($this->server($type), $this->server('SCRIPT_NAME'))) ? substr($this->server($type), strlen($this->server('SCRIPT_NAME'))) : $this->server($type); break; } } } if (!empty($pathinfo)) { unset($this->get[$pathinfo], $this->request[$pathinfo]); } $this->pathinfo = empty($pathinfo) || '/' == $pathinfo ? '' : ltrim($pathinfo, '/'); } return $this->pathinfo; }
第六步:将服务器上的Nginx关闭!!
将服务器上的Nginx关闭!!
将服务器上的Nginx关闭!!
将服务器上所有占用80端口的服务全部关闭!!
将服务器上所有占用80端口的服务全部关闭!!
将服务器上所有占用80端口的服务全部关闭!!
第七步:在服务器终端进入项目目录执行 php think swoole:server -p 80
第八步:index/index/index 代码如下,自己随便写点和swoole有关的,方便测试
<?php namespace app\index\controller; use Swoole\Event; use Swoole\Runtime; use think\Controller; use think\facade\App; class Index extends Controller { public function index() { Runtime::enableCoroutine(); go(function () { sleep(2); file_put_contents(App::getRootPath() . '/1.txt', '123'); }); go(function () { sleep(1); file_put_contents(App::getRootPath() . '/1.txt', '46'); }); $this->assign('path', App::getRootPath() . '/1.txt'); // Event::wait(); return $this->view->fetch(); } }
执行结果为:浏览器访问 http://swool.com/ => 浏览器页面先显示出内容,1秒后根目录下的1.txt 内容先变为46,再1秒后会变为123,说明swoole异步执行成功!
重点在于以下代码:可理解为将原本PHP会阻塞的IO操作变为异步执行
use Swoole\Runtime; Runtime::enableCoroutine();
发表评论