控制器
功能介紹
毫無疑問,控制器層是負責(zé)處理客戶端請求,轉(zhuǎn)發(fā)給響應(yīng)模型,并將結(jié)果返回給客戶端。EasySwoole
使用了對象池復(fù)用模式,降低對象創(chuàng)建、銷毀的開銷,注入 request
和 response
對象來完成客戶端與服務(wù)端之間的交互。
示例
在
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 參數(shù)
$name = $this->request()->getQueryParam('name');
// 輸出到終端
var_dump($name);
// 返回給客戶端
$this->response()->write($name . PHP_EOL);
// return 返回的值會讓框架在此進行控制器方法調(diào)度,將繼續(xù)執(zhí)行 User 控制器類的 requestTotal 方法
return '/User/requestTotal';
}
/**
* 接口請求量
*
* CreateTime: 2020/8/19 12:37 上午
*/
public function requestTotal()
{
$this->response()->write('請求數(shù)+1' . PHP_EOL);
// 還可以 return,但不要兩個方法互相調(diào)用,會導(dǎo)致死循環(huán)
}
/**
* 此控制器拋異常時會執(zhí)行此方法
*
* @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 方法將在執(zhí)行完 afterAction 方法之后自動調(diào)用,可自行覆蓋實現(xiàn)其他的 gc 回收邏輯
*
* CreateTime: 2020/8/19 12:52 上午
*/
public function gc()
{
parent::gc(); // TODO: Change the autogenerated stub
}
/**
* 當(dāng)控制器方法執(zhí)行結(jié)束之后將調(diào)用該方法,可自行覆蓋該方法實現(xiàn)數(shù)據(jù)回收等邏輯
*
* @param string|null $actionName
* CreateTime: 2020/8/19 12:51 上午
*/
public function afterAction(?string $actionName): void
{
parent::afterAction($actionName); // TODO: Change the autogenerated stub
}
/**
* 當(dāng)請求方法未找到時,自動調(diào)用該方法,可自行覆蓋該方法實現(xiàn)自己的邏輯
*
* @param string|null $action
* CreateTime: 2020/8/19 12:51 上午
*/
public function actionNotFound(?string $action)
{
parent::actionNotFound($action); // TODO: Change the autogenerated stub
}
/**
* 所有控制器請求都會先經(jīng)過該方法,如果此方法返回 false 則請求不繼續(xù)往下執(zhí)行,可用于權(quán)限驗證
*
* @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
}
}
執(zhí)行過程
啟動 easyswoole
php easyswoole.php server start
訪問
curl http://localhost:9501/user/userInfo?name=easyswoole
執(zhí)行結(jié)果
服務(wù)端輸出
? 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
請求數(shù)+1
控制器方法
easyswoole 在控制器基類中實現(xiàn)了幾個通用方法,當(dāng)然用戶也可根據(jù)需要進行方法重寫實現(xiàn)自己的邏輯
onRequest
所有控制器請求都會先經(jīng)過該方法,如果此方法返回
false
則請求不繼續(xù)往下執(zhí)行,可用于權(quán)限驗證
protected function onRequest(?string $action): ?bool
{
return true;
}
onException
當(dāng)執(zhí)行控制器方法拋異常時會調(diào)用該方法,可自行覆蓋該方法實現(xiàn)異常捕獲等邏輯
protected function onException(\Throwable $throwable): void
{
throw $throwable;
}
afterAction
當(dāng)
action
執(zhí)行結(jié)束后調(diào)用該方法,可自行覆蓋該方法實現(xiàn)數(shù)據(jù)回收等邏輯
protected function afterAction(?string $actionName): void
{
}
actionNotFound
當(dāng)請求方法未找到時,自動調(diào)用此方法
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 方法執(zhí)行完后調(diào)用
protected function gc()
{
//恢復(fù)默認(rèn)值
foreach ($this->defaultProperties as $property => $value) {
$this->{$property} = $value;
}
}
注意事項
- 只有第一次請求時才會調(diào)用構(gòu)造函數(shù)
- 對象池模式只重置非靜態(tài) public 屬性
- 對象池復(fù)用模式只針對單一進程,多個
worker
進程不共享 - 文件夾、文件、類名為大駝峰,變量與類方法小駝峰(規(guī)范)
-
action
返回的字符串將會被url
解析規(guī)則以及route
路由規(guī)則解析 - 兩個
action
的return
不能互相調(diào)用,否則將導(dǎo)致死循環(huán)
另外注意:在控制器類的方法(onRequest/action
等方法)中創(chuàng)建子協(xié)程,在子協(xié)程中使用 $this
的相關(guān)屬性值時必須使用 use
引入,不使用 use
引入時將導(dǎo)致協(xié)程上下文數(shù)據(jù)錯亂。
錯誤使用示例:
下面以在 Index
控制器類中的 action(index)
中使用為示例:
<?php
/**
* This file is part of EasySwoole.
*
* @link http://www.zbjtqy.com
* @document http://www.zbjtqy.com
* @contact http://www.zbjtqy.com/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()
{
// 設(shè)置請求標(biāo)識
$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);
// 【這里的數(shù)據(jù)會錯亂】
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(示例請求地址)
,發(fā)現(xiàn)出現(xiàn)如下運行結(jié)果,控制臺輸出結(jié)果:
string(21) "第 1 次請求:765"
string(21) "第 1 次請求:765"
string(21) "第 2 次請求:823"
string(21) "第 1 次請求:823"
string(21) "第 2 次請求:823"
string(21) "第 2 次請求:823"
發(fā)現(xiàn)和我們想象中的完全不一樣,第 1
次請求掛載的數(shù)據(jù)被“污染”了,因為 EasySwoole
控制器采用的是對象池模式。
正確使用方式如下:
<?php
/**
* This file is part of EasySwoole.
*
* @link http://www.zbjtqy.com
* @document http://www.zbjtqy.com
* @contact http://www.zbjtqy.com/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()
{
// 設(shè)置請求標(biāo)識
$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);
// 【這里的數(shù)據(jù)會錯亂】
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(示例請求地址)
,發(fā)現(xiàn)出現(xiàn)如下運行結(jié)果,控制臺輸出結(jié)果:
string(21) "第 1 次請求:690"
string(21) "第 1 次請求:690"
string(21) "第 2 次請求:820"
string(21) "第 1 次請求:690"
string(21) "第 2 次請求:820"
string(21) "第 2 次請求:820"
發(fā)現(xiàn)數(shù)據(jù)正常了。