控制器
功能介紹
毫無疑問,控制器層是負責處理客戶端請求,轉發給響應模型,并將結果返回給客戶端。EasySwoole
使用了對象池復用模式,降低對象創建、銷毀的開銷,注入 request
和 response
對象來完成客戶端與服務端之間的交互。
示例
在
App/HttpController/
目錄下增加文件 User.php
代碼
<?php
/**
* @CreateTime: 2020/8/19 12:30 上午
* @Author: huizhang <2788828128@qq.com>
* @Copyright: copyright(2020) Easyswoole all rights reserved
* @Description: 用戶控制器
*/
namespace App\HttpController;
use EasySwoole\Http\AbstractInterface\Controller;
class User extends Controller
{
/**
* 用戶信息
*
* @return string
* CreateTime: 2020/8/19 12:37 上午
*/
public function userInfo()
{
// 獲取 get 參數
$name = $this->request()->getQueryParam('name');
// 輸出到終端
var_dump($name);
// 返回給客戶端
$this->response()->write($name . PHP_EOL);
// return 返回的值會讓框架在此進行控制器方法調度,將繼續執行 User 控制器類的 requestTotal 方法
return '/User/requestTotal';
}
/**
* 接口請求量
*
* CreateTime: 2020/8/19 12:37 上午
*/
public function requestTotal()
{
$this->response()->write('請求數+1' . PHP_EOL);
// 還可以 return,但不要兩個方法互相調用,會導致死循環
}
/**
* 此控制器拋異常時會執行此方法
*
* @param \Throwable $throwable
* @throws \Throwable
* CreateTime: 2020/8/19 12:48 上午
*/
public function onException(\Throwable $throwable): void
{
parent::onException($throwable); // TODO: Change the autogenerated stub
}
/**
* gc 方法將在執行完 afterAction 方法之后自動調用,可自行覆蓋實現其他的 gc 回收邏輯
*
* CreateTime: 2020/8/19 12:52 上午
*/
public function gc()
{
parent::gc(); // TODO: Change the autogenerated stub
}
/**
* 當控制器方法執行結束之后將調用該方法,可自行覆蓋該方法實現數據回收等邏輯
*
* @param string|null $actionName
* CreateTime: 2020/8/19 12:51 上午
*/
public function afterAction(?string $actionName): void
{
parent::afterAction($actionName); // TODO: Change the autogenerated stub
}
/**
* 當請求方法未找到時,自動調用該方法,可自行覆蓋該方法實現自己的邏輯
*
* @param string|null $action
* CreateTime: 2020/8/19 12:51 上午
*/
public function actionNotFound(?string $action)
{
parent::actionNotFound($action); // TODO: Change the autogenerated stub
}
/**
* 所有控制器請求都會先經過該方法,如果此方法返回 false 則請求不繼續往下執行,可用于權限驗證
*
* @param string|null $action
* @return bool|null
* CreateTime: 2020/8/19 12:52 上午
*/
public function onRequest(?string $action): ?bool
{
return parent::onRequest($action); // TODO: Change the autogenerated stub
}
}
執行過程
啟動 easyswoole
php easyswoole.php server start
訪問
curl http://localhost:9501/user/userInfo?name=easyswoole
執行結果
服務端輸出
? doc-new git:(master) ? php easyswoole.php server start
#!/usr/bin/env php
______ _____ _
| ____| / ____| | |
| |__ __ _ ___ _ _ | (___ __ __ ___ ___ | | ___
| __| / _` | / __| | | | | \___ \ \ \ /\ / / / _ \ / _ \ | | / _ \
| |____ | (_| | \__ \ | |_| | ____) | \ V V / | (_) | | (_) | | | | __/
|______| \__,_| |___/ \__, | |_____/ \_/\_/ \___/ \___/ |_| \___|
__/ |
|___/
main server SWOOLE_WEB
listen address 0.0.0.0
listen port 9501
worker_num 8
reload_async true
max_wait_time 3
document_root /Users/guoyuzhao/sites/doc-new/Static
enable_static_handler true
pid_file /Users/guoyuzhao/sites/doc-new/Temp/pid.pid
log_file /Users/guoyuzhao/sites/doc-new/Log/swoole.log
user guoyuzhao
swoole version 4.5.2
php version 7.4.8
easyswoole version 3.4.0-dev
run mode dev
temp dir /Users/guoyuzhao/sites/doc-new/Temp
log dir /Users/guoyuzhao/sites/doc-new/Log
string(10) "easyswoole"
客戶端輸出
? ssh curl http://localhost:9501/user/userInfo\?name\=easyswoole
easyswoole
請求數+1
控制器方法
easyswoole 在控制器基類中實現了幾個通用方法,當然用戶也可根據需要進行方法重寫實現自己的邏輯
onRequest
所有控制器請求都會先經過該方法,如果此方法返回
false
則請求不繼續往下執行,可用于權限驗證
protected function onRequest(?string $action): ?bool
{
return true;
}
onException
當執行控制器方法拋異常時會調用該方法,可自行覆蓋該方法實現異常捕獲等邏輯
protected function onException(\Throwable $throwable): void
{
throw $throwable;
}
afterAction
當
action
執行結束后調用該方法,可自行覆蓋該方法實現數據回收等邏輯
protected function afterAction(?string $actionName): void
{
}
actionNotFound
當請求方法未找到時,自動調用此方法
protected function actionNotFound(?string $action)
{
$class = static::class;
$this->writeJson(\EasySwoole\Http\Message\Status::CODE_NOT_FOUND,null,"{$class} has not action for {$action}");
}
gc
gc 方法在 afterAction 方法執行完后調用
protected function gc()
{
//恢復默認值
foreach ($this->defaultProperties as $property => $value) {
$this->{$property} = $value;
}
}
注意事項
- 只有第一次請求時才會調用構造函數
- 對象池模式只重置非靜態 public 屬性
- 對象池復用模式只針對單一進程,多個
worker
進程不共享 - 文件夾、文件、類名為大駝峰,變量與類方法小駝峰(規范)
-
action
返回的字符串將會被url
解析規則以及route
路由規則解析 - 兩個
action
的return
不能互相調用,否則將導致死循環
另外注意:在控制器類的方法(onRequest/action
等方法)中創建子協程,在子協程中使用 $this
的相關屬性值時必須使用 use
引入,不使用 use
引入時將導致協程上下文數據錯亂。
錯誤使用示例:
下面以在 Index
控制器類中的 action(index)
中使用為示例:
<?php
/**
* This file is part of EasySwoole.
*
* @link http://www.fe88.cn
* @document http://www.fe88.cn
* @contact http://www.fe88.cn/Preface/contact.html
* @license https://github.com/easy-swoole/easyswoole/blob/3.x/LICENSE
*/
namespace App\HttpController;
use EasySwoole\Http\AbstractInterface\Controller;
use EasySwoole\Utility\Random;
class Index extends Controller
{
public function index()
{
// 設置請求標識
$requestFlag = Random::number(3);
$this->request()->withAttribute('requestFlag', $requestFlag);
$rq = '第 ' . $this->request()->getRequestParam('times') . ' 次請求:';
var_dump($rq . $this->request()->getAttribute('requestFlag'));
go(function () {
$rq = '第 ' . $this->request()->getRequestParam('times') . ' 次請求:';
go(function () {
$rq = '第 ' . $this->request()->getRequestParam('times') . ' 次請求:';
\co::sleep(2);
var_dump($rq . $this->request()->getAttribute('requestFlag'));
});
\co::sleep(4);
// 【這里的數據會錯亂】
var_dump($rq . $this->request()->getAttribute('requestFlag'));
});
$this->response()->write('this is index!' . $this->request()->getRequestParam('times'));
}
}
然后我們訪問 http://127.0.0.1:9501/?times=1(示例請求地址)
,隔 1s
后我們再次訪問 http://127.0.0.1:9501/?times=2(示例請求地址)
,發現出現如下運行結果,控制臺輸出結果:
string(21) "第 1 次請求:765"
string(21) "第 1 次請求:765"
string(21) "第 2 次請求:823"
string(21) "第 1 次請求:823"
string(21) "第 2 次請求:823"
string(21) "第 2 次請求:823"
發現和我們想象中的完全不一樣,第 1
次請求掛載的數據被“污染”了,因為 EasySwoole
控制器采用的是對象池模式。
正確使用方式如下:
<?php
/**
* This file is part of EasySwoole.
*
* @link http://www.fe88.cn
* @document http://www.fe88.cn
* @contact http://www.fe88.cn/Preface/contact.html
* @license https://github.com/easy-swoole/easyswoole/blob/3.x/LICENSE
*/
namespace App\HttpController;
use EasySwoole\Http\AbstractInterface\Controller;
use EasySwoole\Utility\Random;
class Index extends Controller
{
public function index()
{
// 設置請求標識
$requestFlag = Random::number(3);
$this->request()->withAttribute('requestFlag', $requestFlag);
$rq = '第 ' . $this->request()->getRequestParam('times') . ' 次請求:';
var_dump($rq . $this->request()->getAttribute('requestFlag'));
go(function () use ($rq, $requestFlag) {
go(function () use ($rq, $requestFlag) {
\co::sleep(2);
var_dump($rq . $requestFlag);
});
\co::sleep(4);
// 【這里的數據會錯亂】
var_dump($rq . $requestFlag);
});
$this->response()->write('this is index!' . $this->request()->getRequestParam('times'));
}
}
然后我們訪問 http://127.0.0.1:9501/?times=1(示例請求地址)
,隔 1s
后我們再次訪問 http://127.0.0.1:9501/?times=2(示例請求地址)
,發現出現如下運行結果,控制臺輸出結果:
string(21) "第 1 次請求:690"
string(21) "第 1 次請求:690"
string(21) "第 2 次請求:820"
string(21) "第 1 次請求:690"
string(21) "第 2 次請求:820"
string(21) "第 2 次請求:820"
發現數據正常了。