kovagoz / http-responder
创建PSR-7兼容HTTP响应的简单解决方案。
1.0.0
2021-09-22 20:52 UTC
Requires
- php: ^8.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.0
Requires (Dev)
- nyholm/psr7: ^1.4
- phpunit/phpunit: ^9.4.0
- roave/security-advisories: dev-latest
This package is auto-updated.
Last update: 2024-09-23 04:10:35 UTC
README
创建PSR-7兼容HTTP响应的简单解决方案。
需求
- PHP >=8.0
使用方法
实例化类
$responder = new HttpResponder($responseFactory, $streamFactory);
创建HTML响应
$response = $responder->reply('hello world');
创建JSON响应
$response = $responder->reply(['foo' => 'bar']);
在这种情况下,响应体将是以下内容
{ "foo": "bar" }
此外,Content-Type
头将被设置为application/json
。
创建空响应(204 No Content)
$response = $responder->reply();
创建重定向
$response = $responder->redirect('https://example.com/');
默认状态码是302
。您可以通过响应对象的withStatus()
方法更改它。
您还可以将UriInterface
对象传递给redirect()
方法,而不是字符串URL。
测试
此存储库包含一个Makefile,可以帮助您在计算机上使用Docker容器运行单元测试。
只需运行以下命令,然后坐下来观看结果。
make test