常见问题

如何获取$HTTP_RAW_POST_DATA

$content = $this->request()->getBody()->__toString();
$raw_array = json_decode($content, true);

如何获取客户端IP

举例,如何在控制器中获取客户端IP

//真实地址
$ip = ServerManager::getInstance()->getSwooleServer()->connection_info($this->request()->getSwooleRequest()->fd);
var_dump($ip);
//header 地址,例如经过nginx proxy后
$ip2 = $this->request()->getHeaders();
var_dump($ip2);

HTTP 状态码总为500

自 swoole 1.10.x2.1.x 版本起,执行http server回调中,若未执行response->end(),则全部返回500状态码

如何setCookie

调用response对象的setCookie方法即可设置cookie

  $this->response()->setCookie('name','value');

更多操作可看Response对象

如何自定义App名称

只需要修改composer.json的命名空间注册就行

    "autoload": {
        "psr-4": {
            "App\\": "Application/"
        }
    }

关于跨域处理

在全局事件添加以下代码 拦截所有请求添加跨域头

public static function onRequest(Request $request, Response $response): bool
{
    // TODO: Implement onRequest() method.
    $response->withHeader('Access-Control-Allow-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');
    if ($request->getMethod() === 'OPTIONS') {
        $response->withStatus(Status::CODE_OK);
        return false;
    }
    return true;
}

0 results matching ""

    No results matching ""