# PSR 7 与值对象(Value Objects)
*****
Slim supports[PSR-7](https://github.com/php-fig/http-message)interfaces for its Request and Response objects. This makes Slim flexible because it can use*any*PSR-7 implementation. For example, you could return an instance of`GuzzleHttp\Psr7\CachingStream`or any instance returned by the`GuzzleHttp\Psr7\stream_for()`function.
> Slim为其请求和响应对象支持[PSR-7](https://github.com/php-fig/http-message)接口。这使得Slim非常灵活,因为它可以使用任何PSR-7实现。例如,您可以返回 `GuzzleHttp\Psr7\CachingStream` 的实例,或者 `GuzzleHttp\Psr7\stream_for()` 函数返回的任何实例。
Slim provides its own PSR-7 implementation so that it works out of the box. However, you are free to replace Slim’s default PSR-7 objects with a third-party implementation. Just override the application container’s`request`and`response`services so they return an instance of`Psr\Http\Message\ServerRequestInterface`and`Psr\Http\Message\ResponseInterface`, respectively.
Slim提供了自己的PSR-7实现,因此可以开箱即用。但是,您可以使用第三方实现替换Slim的默认PSR-7对象。只需覆盖应用程序容器的`请求`和`响应`服务,以便它们分别返回 `Psr\Http\Message\ServerRequestInterface` 和 `Psr\Http\Message\ResponseInterface` 的实例。
# 值对象
Request and Response objects are[*immutable value objects*](http://en.wikipedia.org/wiki/Value_object). They can be “changed” only by requesting a cloned version that has updated property values. Value objects have a nominal overhead because they must be cloned when their properties are updated. This overhead does not affect performance in any meaningful way.
> 请求和响应对象是[*immutable value objects*](http://en.wikipedia.org/wiki/Value_object)。只能通过请求具有更新属性值的克隆版本来“更改”它们。值对象有名义上的开销,因为必须在更新其属性时克隆它们。这种开销不会以任何有意义的方式影响性能。
You can request a copy of a value object by invoking any of its PSR-7 interface methods (these methods typically have a`with`prefix). For example, a PSR-7 Response object has a`withHeader($name, $value)`method that returns a cloned value object with the new HTTP header.
> 您可以通过调用值对象的任何PSR-7接口方法(这些方法通常具有`with` 前缀)来请求值对象的副本。例如,一个PSR-7响应对象有一个 `withHeader($name, $value)` 方法,该方法用新的HTTP头返回一个克隆的值对象。
~~~php
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
require __DIR__ . '/../vendor/autoload.php';
$app = AppFactory::create();
$app->get('/foo', function (Request $request, Response $response, array $args) {
$payload = json_encode(['hello' => 'world'], JSON_PRETTY_PRINT);
$response->getBody()->write($payload);
return $response->withHeader('Content-Type', 'application/json');
});
$app->run();
~~~
> PSR-7接口提供了这些方法来转换请求和响应对象:
* `withProtocolVersion($version)`
* `withHeader($name, $value)`
* `withAddedHeader($name, $value)`
* `withoutHeader($name)`
* `withBody(StreamInterface $body)`
> PSR-7接口提供了这些方法来转换请求对象:
* `withMethod($method)`
* `withUri(UriInterface $uri, $preserveHost = false)`
* `withCookieParams(array $cookies)`
* `withQueryParams(array $query)`
* `withUploadedFiles(array $uploadedFiles)`
* `withParsedBody($data)`
* `withAttribute($name, $value)`
* `withoutAttribute($name)`
> PSR-7接口提供了这些方法来转换响应对象:
* `withStatus($code, $reasonPhrase = '') `
> 有关这些方法的更多信息,请参阅[PSR-7文档](http://www.php-fig.org/psr/psr-7/)。
[ PSR-7 HTTP 消息接口规范翻译文档](https://learnku.com/docs/psr/psr-7-http-message/1616)
- 开始
- 安装
- 升级指南
- Web服务器
- 概念
- 生命周期
- PSR 7
- 中间件
- 依赖容器
- 实例 及通知和警告处理
- Request
- 请求方法
- 请求头信息
- 请求主体
- 上传的文件
- 请求帮助
- 路由对象
- Response
- 响应状态
- 响应标头
- 响应体
- 返回JSON
- 视图模板
- 路由
- 创建路由
- 路由回调
- 路由策略
- 路线占位符
- 路由名
- 路由组
- 路由中间件
- 路由表达式缓存
- 容器识别解析
- 封装中间件
- 路由的中间件
- 错误处理中间件
- 方法重写的中间件
- 输出缓冲中间件
- 内容长度中间件
- 扩展功能
- 以 / 结尾的路由模式
- 获取当前路由
- 设置CORS
- 使用POST表单上传文件
- 第三方组件
- slim-session
- auth
- slim-api-skeleton
- dir