onRequest 事件(即收到請求事件)
使用場景及原理
當 EasySwoole
收到任何的 HTTP
請求時,均會執行該事件。可以使用該事件可以對 HTTP
請求全局攔截,包括對請求進行允許跨域等操作。
使用方式說明
框架對 onRequest
事件的實現在 3.4.x 及以后的版本
中做了新的改動,實現方式由原來舊版本在主服務創建事件(mainServerCreate 事件
)中定義改變為在 initialize 事件 中使用 Di
方式注入。目前最新穩定版本框架(3.4.x
),具體實現及使用方式 (在 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');
// 實現 onRequest 事件
\EasySwoole\Component\Di::getInstance()->set(\EasySwoole\EasySwoole\SysConst::HTTP_GLOBAL_ON_REQUEST, function (\EasySwoole\Http\Request $request, \EasySwoole\Http\Response $response): bool {
###### 對請求進行攔截 ######
// 不建議在這攔截請求,可增加一個控制器基類進行攔截
// 如果真要攔截,判斷之后 return false; 即可
/*
$code = $request->getRequestParam('code');
if (0){ // empty($code)驗證失敗
$data = array(
"code" => \EasySwoole\Http\Message\Status::CODE_BAD_REQUEST,
"result" => [],
"msg" => '驗證失敗'
);
$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;
*/
###### 處理請求的跨域問題 ######
$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
事件的實現如下所示:
<?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)
{
}
// 注冊 onRequest 事件回調
public static function onRequest(Request $request, Response $response): bool
{
###### 對請求進行攔截 ######
// 不建議在這攔截請求,可增加一個控制器基類進行攔截
// 如果真要攔截,判斷之后 return false; 即可
/*
$code = $request->getRequestParam('code');
if (0){ // empty($code)驗證失敗
$data = array(
"code" => \EasySwoole\Http\Message\Status::CODE_BAD_REQUEST,
"result" => [],
"msg" => '驗證失敗'
);
$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;
*/
###### 處理請求的跨域問題 ######
$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;
}
}
注意事項
若在該事件中,執行 $response->end()
,則該次請求不會進入路由匹配階段。