日韩在线观看-日韩在线成人-日韩在线不卡视频-日韩在线不卡视频-国产精品99-国产精品99

onRequest 事件(即收到請求事件)

使用場景及原理

當(dāng) EasySwoole 收到任何的 HTTP 請求時,均會執(zhí)行該事件。可以使用該事件可以對 HTTP 請求全局?jǐn)r截,包括對請求進(jìn)行允許跨域等操作。

使用方式說明

框架對 onRequest 事件的實現(xiàn)在 3.4.x 及以后的版本 中做了新的改動,實現(xiàn)方式由原來舊版本在主服務(wù)創(chuàng)建事件(mainServerCreate 事件)中定義改變?yōu)樵?initialize 事件 中使用 Di 方式注入。目前最新穩(wěn)定版本框架(3.4.x),具體實現(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');

        // 實現(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 {
            ###### 對請求進(jìn)行攔截 ######
            // 不建議在這攔截請求,可增加一個控制器基類進(jìn)行攔截
            // 如果真要攔截,判斷之后 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 事件的實現(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)
    {

    }

    // 注冊 onRequest 事件回調(diào)
    public static function onRequest(Request $request, Response $response): bool
    {
        ###### 對請求進(jìn)行攔截 ######
        // 不建議在這攔截請求,可增加一個控制器基類進(jìn)行攔截
        // 如果真要攔截,判斷之后 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;
    }
}

注意事項

若在該事件中,執(zhí)行 $response->end(),則該次請求不會進(jìn)入路由匹配階段。

主站蜘蛛池模板: 母与子| 难兄难弟电视剧| 第一财经电视| 烽火流金电视剧免费观看完整版| 奖励员工的表扬通告怎么写| 电视剧《唐太宗李世民》| 日不落酒店 电影| 刘浩存个人资料简介图片| 还珠格格第一部主题曲| 大森南朋| 45分钟见奶薄纱透明时装秀| 鏖战鲁西南| 重温经典频道节目表| 世界赠予我的钢琴曲| 电影《很差劲》在线观看| 陈稳| 特殊的按摩| 嫦娥奔月读后感50字| 黄视频在线网站| 寡妇一级毛片免费看| 辩论稿| 罗伯特·杜瓦尔| 五年级语文下册| 极品美女在线| 烽火流金电视剧全集免费观看| 局中局演员表| austin rivers| 富含维生素c的水果和蔬菜| 秀人网美女屋| 河南省物业管理条例| 宝宝乐园| 秀人网 官网门户免费| 变形金刚6免费观看高清完整版| 杨幂三级| 青草国产视频| 电影双面情人| 张钰凰| 职业兽医医生资格证报名官网| one week| 超人演员| 滕子萱|