onRequest 事件(即收到請(qǐng)求事件)
使用場景及原理
當(dāng) EasySwoole
收到任何的 HTTP
請(qǐng)求時(shí),均會(huì)執(zhí)行該事件。可以使用該事件可以對(duì) HTTP
請(qǐng)求全局?jǐn)r截,包括對(duì)請(qǐng)求進(jìn)行允許跨域等操作。
使用方式說明
框架對(duì) onRequest
事件的實(shí)現(xiàn)在 3.4.x 及以后的版本
中做了新的改動(dòng),實(shí)現(xiàn)方式由原來舊版本在主服務(wù)創(chuàng)建事件(mainServerCreate 事件
)中定義改變?yōu)樵?initialize 事件 中使用 Di
方式注入。目前最新穩(wěn)定版本框架(3.4.x
),具體實(shí)現(xiàn)及使用方式 (在 EasySwooleEvent.php
中的 initialize
事件中注入) 如下:
<?php
namespace EasySwoole\EasySwoole;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\EasySwoole\Swoole\EventRegister;
class EasySwooleEvent implements Event
{
public static function initialize()
{
date_default_timezone_set('Asia/Shanghai');
// 實(shí)現(xiàn) onRequest 事件
\EasySwoole\Component\Di::getInstance()->set(\EasySwoole\EasySwoole\SysConst::HTTP_GLOBAL_ON_REQUEST, function (\EasySwoole\Http\Request $request, \EasySwoole\Http\Response $response): bool {
###### 對(duì)請(qǐng)求進(jìn)行攔截 ######
// 不建議在這攔截請(qǐng)求,可增加一個(gè)控制器基類進(jìn)行攔截
// 如果真要攔截,判斷之后 return false; 即可
/*
$code = $request->getRequestParam('code');
if (0){ // empty($code)驗(yàn)證失敗
$data = array(
"code" => \EasySwoole\Http\Message\Status::CODE_BAD_REQUEST,
"result" => [],
"msg" => '驗(yàn)證失敗'
);
$response->write(json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
$response->withHeader('Content-type', 'application/json;charset=utf-8');
$response->withStatus(\EasySwoole\Http\Message\Status::CODE_BAD_REQUEST);
return false;
}
return true;
*/
###### 處理請(qǐng)求的跨域問題 ######
$origin = $request->getHeaderLine('origin') ?: '*';
$response->withHeader('Access-Control-Allow-Origin', $origin);
$response->withHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
$response->withHeader('Access-Control-Allow-Credentials', 'true');
$response->withHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With, token');
if ($request->getMethod() === 'OPTIONS') {
$response->withStatus(\EasySwoole\Http\Message\Status::CODE_OK);
return false;
}
return true;
});
}
public static function mainServerCreate(EventRegister $register)
{
}
}
舊版本(3.4.x
之前版本)框架的 onRequest
事件的實(shí)現(xiàn)如下所示:
<?php
namespace EasySwoole\EasySwoole;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\Http\Request;
use EasySwoole\Http\Response;
class EasySwooleEvent implements Event
{
public static function initialize()
{
// TODO: Implement initialize() method.
date_default_timezone_set('Asia/Shanghai');
}
public static function mainServerCreate(EventRegister $register)
{
}
// 注冊(cè) onRequest 事件回調(diào)
public static function onRequest(Request $request, Response $response): bool
{
###### 對(duì)請(qǐng)求進(jìn)行攔截 ######
// 不建議在這攔截請(qǐng)求,可增加一個(gè)控制器基類進(jìn)行攔截
// 如果真要攔截,判斷之后 return false; 即可
/*
$code = $request->getRequestParam('code');
if (0){ // empty($code)驗(yàn)證失敗
$data = array(
"code" => \EasySwoole\Http\Message\Status::CODE_BAD_REQUEST,
"result" => [],
"msg" => '驗(yàn)證失敗'
);
$response->write(json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
$response->withHeader('Content-type', 'application/json;charset=utf-8');
$response->withStatus(\EasySwoole\Http\Message\Status::CODE_BAD_REQUEST);
return false;
}
return true;
*/
###### 處理請(qǐng)求的跨域問題 ######
$origin = $request->getHeaderLine('origin') ?: '*';
$response->withHeader('Access-Control-Allow-Origin', $origin);
$response->withHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
$response->withHeader('Access-Control-Allow-Credentials', 'true');
$response->withHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With, token');
if ($request->getMethod() === 'OPTIONS') {
$response->withStatus(\EasySwoole\Http\Message\Status::CODE_OK);
return false;
}
return true;
}
}
注意事項(xiàng)
若在該事件中,執(zhí)行 $response->end()
,則該次請(qǐng)求不會(huì)進(jìn)入路由匹配階段。