thinkphp5支持php,如何让swoole完美支持ThinkPHP5呢

视频教程推荐

TP5与workerman实战在线客服

首先开启swoole的http服务

开启swoole的http服务并设置提供给url访问页面的文件的根目录,

代码如下。这样在浏览器只能访问…static下面的文件,而无法访问其他目录下的文件。

http.server

set( [ 'enable_static_handler' => true, 'document_root' => "/usr/local/openresty/nginx/html/swoole/LiveRadio/public/static", 'worker_num' => 5 ]);$http->on('request', function($request, $response) { $response->end("sss". json_encode($request->get));});$http->start();swoole有一个事件回调函数onWorkStart,此事件在Worker进程/Task进程启动时发生。这里创建的对象可以在进程生命周期内使用。原型:

function onWorkerStart(swoole_server $server, int workerid);(1)onWorkerStart/onStart是并发执行的,没有先后顺序;

(2)可以通过server->taskworker属性来判断当前是Worker进程还是Task进程;

(3)设置了worker_num和task_worker_num超过1时,每个进程都会触发一次onWorkerStart事件,可通过判断$worker_id区分不同的工作进程;

(4)由 worker 进程向 task 进程发送任务,task 进程处理完全部任务之后通过onFinish回调函数通知worker 进程。

例如,我们在后台操作向十万个用户群发通知邮件,操作完成后操作的状态显示为发送中,这时我们可以继续其他操作。等邮件群发完毕后,操作的状态自动改为已发送。

在onWorkerStart中加载框架的核心文件后:(1)不用每次请求都加载框架核心文件,提高性能;(2)可以在后续的回调事件中继续使用框架的核心文件或者类库。

项目目录下的public文件夹下的index.php是入口文件,源码如下:

修改http.server如下:

set( [ 'enable_static_handler' => true, 'document_root' => "/usr/local/openresty/nginx/html/swoole/LiveRadio/public/static", 'worker_num' => 5 //设置worker进程数 ]);$http->on('WorkerStart', function (swoole_server $server, $worker_id){ //定义应用目录 define('APP_PATH', __DIR__ . '/../application/'); // 加载基础文件 //这里不直接加载start.php的原因是start.php中的代码会直接执行,也就是application\index\controller\Index.php文件(框架的默认首页) /* * Container::get('app', [defined('APP_PATH') ? APP_PATH : '']) ->run() ->send(); */ require __DIR__ . '/../thinkphp/base.php';});$http->on('request', function($request, $response){ //适配 /* *由于swoole http提供的API和原生php代码是有所不同的,比如原生php中获取get参数为直接从全局数组_GET中获取,而swoole http中是通过$request->get()的方式获取,因此要转换成原生的 * */ $_SERVER = []; if(isset($request->server)){ 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_start(); // 执行应用并响应 try { think\Container::get('app', [APP_PATH]) ->run() ->send(); }catch (\Exception $e){ //todo } $res = ob_get_contents(); ob_end_clean(); $response->end($res);});$http->start();此时,用浏览器访问 http://ip:port/?s=index/index/index 可以看到ThinkPHP的默认首页,但是发现访问不到http://ip:port/?s=index/index/hello?name=world页面。

可以打印出路由看看。在项目目录\thinkphp\library\think\App.php文件中的routeCheck接口,打印path变量,可以看到url改变后path变量并没有改变。这是因为onWorkerStart进程加载框架的时候就复用了类成员变量。

/** * URL路由检测(根据PATH_INFO) * @access public * @return Dispatch */ public function routeCheck() { $path = $this->request->path(); echo "path:".$path."
";// var_dump($this->request); $depr = $this->config('app.pathinfo_depr'); // 路由检测 $files = scandir($this->routePath); foreach ($files as $file) { if (strpos($file, '.php')) { $filename = $this->routePath . DIRECTORY_SEPARATOR . $file; // 导入路由配置 $rules = include $filename; if (is_array($rules)) { $this->route->import($rules); } } } // 是否强制路由模式 $must = !is_null($this->routeMust) ? $this->routeMust : $this->config('app.url_route_must'); // 路由检测 返回一个Dispatch对象 return $this->route->check($path, $depr, $must, $this->config('app.route_complete_match')); }修改:

进入 项目目录\thinkphp\library\think\Request.php中

(1)找到function path() { },注销判断,不再复用类成员变量 $ this->path。

(2)找到function pathinfo() { },注销判断,不再复用类成员变量$this->pathinfo。

(3)使其支持pathinfo路由,在function pathinfo() { }中添加代码

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

thinkphp5支持php,如何让swoole完美支持ThinkPHP5呢_第1张图片

此时用浏览器访问:

thinkphp5支持php,如何让swoole完美支持ThinkPHP5呢_第2张图片

thinkphp5支持php,如何让swoole完美支持ThinkPHP5呢_第3张图片

你可能感兴趣的:(thinkphp5支持php)