ft / request-response
一个包含常用请求响应工具和辅助方法的库
2.0.1
2023-02-14 03:10 UTC
Requires
- php: >=8.1
Requires (Dev)
- creativestyle/app-http-server-mock: ^1.0
- guzzlehttp/guzzle: ^7.4
- phpunit/phpunit: ^9.5
- symfony/http-foundation: ^6.1
- symfony/process: ^6.1
README
composer require ft/request-response
用法
请求
$req = new Request;
就这样。基于 $_SERVER 和请求方法,请求会自动构建所有相关内容。
所有参数都添加到请求的 parameters 属性中,无论它们是查询参数还是通过 multipart/form-data 或 www-form-urlencoded 发送的表单参数。(尽管,这些也被添加到 body 属性中)
响应
$resp = new Response();
Response 是一个构建模式类
示例
$resp = new Response(); $resp->statusCode(StatusCodes::HTTP_VERSION_NOT_SUPPORTED) ->send();
每次你调用 Response 的 send*() 方法时,它都会调用 die()
Response 会识别内容类型,例如,如果你调用 sendJson() 方法,它将自动为你设置内容类型头
$array = [ 'foo' => 'bar' ]; $resp = new Response(); $resp->sendJson($array);