このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン 次のリビジョン 両方とも次のリビジョン | ||
slim:4:response [2020/10/06 17:06] y2sunlight [How to get the Response object] |
slim:4:response [2020/10/07 21:14] y2sunlight |
||
---|---|---|---|
行 1: | 行 1: | ||
- | > 編集中 | ||
- | |||
====== Slim4 レスポンス ====== | ====== Slim4 レスポンス ====== | ||
Version 4.5.0 | Version 4.5.0 | ||
行 51: | 行 49: | ||
\\ | \\ | ||
- | ===== The Response Status | + | ===== レスポンスステータス |
- | Every HTTP response has a numeric status code. The status code identifies the type of HTTP response to be returned to the client. The PSR-7 Response object’s default status code is '' | + | すべてのHTTPレスポンスには、数値のステータスコードがあります。[[https:// |
- | + | ||
- | すべてのHTTP応答には、数値のステータスコードがあります。[[https:// | + | |
<code php> | <code php> | ||
行 61: | 行 57: | ||
</ | </ | ||
- | You can copy a PSR-7 Response object and assign a new status code like this: | + | PSR-7レスポンスオブジェクトをコピーして、次のような新しいステータスコードを割り当てることができます: |
- | + | ||
- | PSR-7応答オブジェクトをコピーして、次のような新しいステータスコードを割り当てることができます。 | + | |
<code php> | <code php> | ||
行 71: | 行 65: | ||
\\ | \\ | ||
- | ===== The Response Headers | + | ===== レスポンスヘッダー |
- | + | ||
- | Every HTTP response has headers. These are metadata that describe the HTTP response but are not visible in the response’s body. The PSR-7 Response object provides several methods to inspect and manipulate its headers. | + | |
- | + | ||
- | すべてのHTTP応答にはヘッダーがあります。 これらはHTTP応答を説明するメタデータですが、応答の本文には表示されません。 PSR-7 Responseオブジェクトは、そのヘッダーを検査および操作するためのいくつかのメソッドを提供します。 | + | |
- | ==== Get All Headers ==== | + | 全てのHTTPレスポンスにはヘッダーがあります。これらはHTTPレスポンスを説明するメタデータで、レスポンスのボディには表示されません。PSR-7 レスポンスオブジェクトは、そのヘッダーを検査および操作するためのいくつかのメソッドを提供します。 |
- | You can fetch all HTTP response headers as an associative array with the PSR-7 Response object’s '' | + | ==== 全てのヘッダーを取得する ==== |
- | PSR-7 Responseオブジェクトの「getHeaders()」メソッドを使用して、すべてのHTTP応答ヘッダーを連想配列としてフェッチできます。 結果として得られる連想配列のキーはヘッダー名であり、その値自体がそれぞれのヘッダー名の文字列値の数値配列です。 | + | PSR-7 レスポンスオブジェクトの |
<code php> | <code php> | ||
行 92: | 行 82: | ||
\\ | \\ | ||
- | ==== Get One Header | + | ==== 1つのヘッダーを取得する |
- | You can get a single header’s value(s) with the PSR-7 Response object’s '' | + | PSR-7レスポンスオブジェクトの '' |
- | + | ||
- | PSR-7Responseオブジェクトの '' | + | |
<code php> | <code php> | ||
行 102: | 行 90: | ||
</ | </ | ||
- | You may also fetch a comma-separated string with all values for a given header with the PSR-7 Response object’s '' | + | PSR-7 レスポンスオブジェクトの '' |
- | + | ||
- | PSR-7 Responseオブジェクトの '' | + | |
<code php> | <code php> | ||
行 112: | 行 98: | ||
\\ | \\ | ||
- | ==== Detect Header | + | ==== ヘッダーを検出する |
- | + | ||
- | You can test for the presence of a header with the PSR-7 Response object’s '' | + | |
- | PSR-7Responseオブジェクトの '' | + | PSR-7 レスポンスオブジェクトの '' |
<code php> | <code php> | ||
行 126: | 行 110: | ||
\\ | \\ | ||
- | ==== Set Header | + | ==== ヘッダーをセットする |
- | You can set a header value with the PSR-7 Response object’s '' | + | PSR-7 レスポンスオブジェクトの '' |
- | + | ||
- | PSR-7Responseオブジェクトの '' | + | |
<code php> | <code php> | ||
行 136: | 行 118: | ||
</ | </ | ||
- | >**Reminder** | + | >**注意** |
- | >The Response object is immutable. This method returns a copy of the Response object that has the new header value. This method is destructive, | + | > |
- | + | ||
- | > | + | |
- | > | + | |
\\ | \\ | ||
- | ==== Append Header | + | ==== ヘッダーを追加する |
- | You can append a header value with the PSR-7 Response | + | PSR-7 Responseオブジェクトの '' |
- | + | ||
- | PSR-7Responseオブジェクトの '' | + | |
<code php> | <code php> | ||
行 154: | 行 131: | ||
</ | </ | ||
- | >**Reminder** | + | > **注意** |
- | >Unlike the withHeader() | + | > withHeader() メソッドとは異なり、このメソッドは、同じヘッダー名として既に存在する値のセットに新しい値を追加します。Response オブジェクトは不変です。このメソッドは、ヘッダー値が追加されたResponseオブジェクトのコピーを返します。 |
- | + | ||
- | > **リマインダー** | + | |
- | > withHeader()メソッドとは異なり、このメソッドは、同じヘッダー名にすでに存在する値のセットに新しい値を追加します。 Responseオブジェクトは不変です。 このメソッドは、ヘッダー値が追加されたResponseオブジェクトのコピーを返します。 | + | |
\\ | \\ | ||
- | ==== Remove Header | + | ==== ヘッダーを削除する |
- | + | ||
- | You can remove a header with the Response object’s '' | + | |
- | Responseオブジェクトの '' | + | Responseオブジェクトの '' |
<code php> | <code php> | ||
行 173: | 行 145: | ||
> **リマインダー** | > **リマインダー** | ||
- | > Responseオブジェクトは不変です。 このメソッドは、ヘッダー値が追加されたResponseオブジェクトのコピーを返します。 | + | > Responseオブジェクトは不変です。 このメソッドは、ヘッダー値を削除したResponseオブジェクトのコピーを返します。 |
\\ | \\ | ||
- | ===== The Response Body ===== | + | ===== レスポンスボディ |
- | An HTTP response typically has a body. | + | HTTPレスポンスには通常、ボディがあります。 |
- | HTTP応答には通常、本文があります。 | + | PSR-7 Requestオブジェクトと同様に、PSR-7 Responseオブジェクトはボディを '' |
- | + | ||
- | Just like the PSR-7 Request object, the PSR-7 Response object implements the body as an instance of '' | + | |
- | + | ||
- | PSR-7 Requestオブジェクトと同様に、PSR-7Responseオブジェクトは本体を「Psr \ Http \ Message \ StreamInterface」のインスタンスとして実装します。 PSR-7 Responseオブジェクトの「getBody()」メソッドを使用して、HTTP応答本体の「StreamInterface」インスタンスを取得できます。 '' | + | |
<code php> | <code php> | ||
行 191: | 行 159: | ||
</ | </ | ||
- | The resultant '' | + | 結果として得られる |
- | + | ||
- | 結果の '' | + | |
* getSize() | * getSize() | ||
行 208: | 行 174: | ||
* getMetadata($key = null) | * getMetadata($key = null) | ||
- | Most often, you’ll need to write to the PSR-7 Response object. You can write content to the '' | + | ほとんどの場合、PSR-7 Responseオブジェクトには書き込む必要があります。次のように、'' |
- | + | ||
- | ほとんどの場合、PSR-7Responseオブジェクトに書き込む必要があります。 次のように、「write()」メソッドを使用して「StreamInterface」インスタンスにコンテンツを書き込むことができます。 | + | |
<code php> | <code php> | ||
行 217: | 行 181: | ||
</ | </ | ||
- | You can also replace the PSR-7 Response | + | PSR-7 Responseオブジェクトのボディをまったく新しい |
- | PSR-7Responseオブジェクトの本体をまったく新しい「StreamInterface」インスタンスに置き換えることもできます。 | + | これは、コンテンツをリモートの宛先(例えば、ファイルシステムやリモートAPIなど)からHTTPレスポンスにパイプ処理する場合に特に便利です。PSR-7 Responseオブジェクトのボディを '' |
<code php> | <code php> | ||
行 228: | 行 192: | ||
</ | </ | ||
- | >**Reminder** | + | > **注意** |
- | >The Response object is immutable. This method returns a copy of the Response object that contains the new body. | + | > Responseオブジェクトは不変です。このメソッドは、新しい本文を含むResponseオブジェクトのコピーを返します。 |
- | + | ||
- | > **リマインダー** | + | |
- | > Responseオブジェクトは不変です。 このメソッドは、新しい本文を含むResponseオブジェクトのコピーを返します。 | + | |
\\ | \\ | ||
- | ===== Returning | + | ===== JSONを返す |
- | In it’s simplest form, JSON data can be returned with a default 200 HTTP status code. | + | 最も単純な形式では、JSONデータはデフォルトの |
- | + | ||
- | 最も単純な形式では、JSONデータはデフォルトの200HTTPステータスコードで返すことができます。 | + | |
<code php> | <code php> | ||
行 250: | 行 209: | ||
-> | -> | ||
</ | </ | ||
- | |||
- | We can also return JSON data with a custom HTTP status code. | ||
カスタムHTTPステータスコードを使用してJSONデータを返すこともできます。 | カスタムHTTPステータスコードを使用してJSONデータを返すこともできます。 | ||
行 265: | 行 222: | ||
</ | </ | ||
- | > | ||
>The Response object is immutable. This method returns a copy of the Response object that has a new Content-Type header. This method is destructive, | >The Response object is immutable. This method returns a copy of the Response object that has a new Content-Type header. This method is destructive, | ||
- | > **リマインダー** | + | > **注意** |
- | > Responseオブジェクトは不変です。 このメソッドは、新しいContent-Typeヘッダーを持つResponseオブジェクトのコピーを返します。 このメソッドは破壊的であり、既存のContent-Typeヘッダーを置き換えます。 | + | > Responseオブジェクトは不変です。このメソッドは、新しいContent-Typeヘッダーを持つResponseオブジェクトのコピーを返します。このメソッドは破壊的であり、既存のContent-Typeヘッダーを置き換えます。 |
\\ | \\ |