dlzer/http-utils

用于PSR-7的实用类和常量

dev-master 2021-10-01 19:36 UTC

This package is auto-updated.

Last update: 2024-09-29 05:36:39 UTC


README

GitHub GitHub release (latest by date)

提供快速参考使用的实用类和常量,用于请求方法和响应。

安装

compose require dlzer/http-utils

使用

当与 PHP-FIG 状态码接口一起使用时。

// Custom responder method
public function withJson(
    ResponseInterface $response,
    $statusCode, 
    $message, 
    $data = null, 
    int $options = 0
    ): ResponseInterface
{
    return $response->write(json_encode([
        "status" => $statusCode,
        "message" => $message,
        "data" => $data
    ], $options);
    );
}

// Usage
$data = ["connection" => true];
return $this->responder->withJson(
    $response, // The response interface
    StatusCodeInterface::STATUS_OK // The status code interface
    StatusCodeMessage::STATUS_OK // The status message interface: "OK"
    $data // The response data
);

输出

{
    "status": 200,
    "message": "OK",
    "data": {
        "connection": true
    }
}