# 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