Ground Sunlight

Windowsで作る - PHPプログラミングの開発環境

ユーザ用ツール

サイト用ツール


slim:4:response

差分

このページの2つのバージョン間の差分を表示します。

この比較画面にリンクする

両方とも前のリビジョン 前のリビジョン
次のリビジョン
前のリビジョン
slim:4:response [2020/10/06 17:30]
y2sunlight [The Response Headers]
slim:4:response [2020/10/07 21:20] (現在)
y2sunlight
行 1: 行 1:
-> 編集中 
- 
 ====== Slim4 レスポンス ====== ====== Slim4 レスポンス ======
 Version 4.5.0 Version 4.5.0
行 16: 行 14:
   * [[slim:4:routing|Slim4 ルーティング]]   * [[slim:4:routing|Slim4 ルーティング]]
   * [[slim:4:middleware|Slim4 ミドルウェア]]   * [[slim:4:middleware|Slim4 ミドルウェア]]
 +  * [[slim:4:cookbook|Slim4  クックブック]]
  
 本章は以下のサイトの **The Response** のセクションを翻訳し若干の補足を加えたのもです。 本章は以下のサイトの **The Response** のセクションを翻訳し若干の補足を加えたのもです。
行 71: 行 70:
 全てのHTTPレスポンスにはヘッダーがあります。これらはHTTPレスポンスを説明するメタデータで、レスポンスのボディには表示されません。PSR-7 レスポンスオブジェクトは、そのヘッダーを検査および操作するためのいくつかのメソッドを提供します。 全てのHTTPレスポンスにはヘッダーがあります。これらはHTTPレスポンスを説明するメタデータで、レスポンスのボディには表示されません。PSR-7 レスポンスオブジェクトは、そのヘッダーを検査および操作するためのいくつかのメソッドを提供します。
  
-==== Get All Headers ====+==== 全てのヘッダーを取得する ====
  
-You can fetch all HTTP response headers as an associative array with the PSR-7 Response object’s ''getHeaders()'' method. The resultant associative array’s keys are the header names and its values are themselves a numeric array of string values for their respective header name. +PSR-7 レスポンスオブジェクトの ''getHeaders()'' メソッドを使用して、すべてのHTTPレスポンスヘッダーを連想配列としてフェッチできます。結果として得られる連想配列のキーはヘッダー名であり、その値自体がそれぞれのヘッダー名の文字列値の数値配列です。
- +
-PSR-7 ResponseオブジェクトのgetHeaders()」メソッドを使用して、すべてのHTTP応答ヘッダーを連想配列としてフェッチできます。 結果として得られる連想配列のキーはヘッダー名であり、その値自体がそれぞれのヘッダー名の文字列値の数値配列です。+
  
 <code php> <code php>
行 86: 行 83:
 \\ \\
  
-==== Get One Header ====+==== 1つのヘッダーを取得する ====
  
-You can get a single header’s value(s) with the PSR-7 Response object’s ''getHeader($name)'' method. This returns an array of values for the given header name. Remember, a single HTTP header may have more than one value! +PSR-7レスポンスオブジェクトの ''getHeader($name)'' メソッドを使用して、単一のヘッダーの値を取得できます。これにより、指定されたヘッダー名の値の配列が返されます。1つのHTTPヘッダーに複数の値が含まれる場合があることに注意してください。
- +
-PSR-7Responseオブジェクトの '' getHeader$ name) ''メソッドを使用して、単一のヘッダーの値を取得できます。 これにより、指定されたヘッダー名の値の配列が返されます。 1つのHTTPヘッダーに複数の値が含まれる場合があることに注意してください。+
  
 <code php> <code php>
行 96: 行 91:
 </code> </code>
  
-You may also fetch a comma-separated string with all values for a given header with the PSR-7 Response object’s ''getHeaderLine($name)'' method. Unlike the ''getHeader($name)'' method, this method returns a comma-separated string. +PSR-7 レスポンスオブジェクトの ''getHeaderLine($name)'' メソッドを使用して、特定のヘッダーのすべての値を含むコンマ区切りの文字列をフェッチすることもできます。''getHeader($name)'' メソッドとは異なり、このメソッドはコンマで区切られた文字列を返します。
- +
-PSR-7 Responseオブジェクトの '' getHeaderLine$ name) ''メソッドを使用して、特定のヘッダーのすべての値を含むコンマ区切りの文字列をフェッチすることもできます。 '' getHeader$ name) ''メソッドとは異なり、このメソッドはコンマで区切られた文字列を返します。+
  
 <code php> <code php>
行 106: 行 99:
 \\ \\
  
-==== Detect Header ==== +==== ヘッダーを検出する ====
- +
-You can test for the presence of a header with the PSR-7 Response object’s ''hasHeader($name)'' method.+
  
-PSR-7Responseオブジェクトの '' hasHeader$ name) ''メソッドを使用して、ヘッダーの存在をテストできます。+PSR-7 レスポンスオブジェクトの ''hasHeader($name)'' メソッドを使用して、ヘッダーの存在をテストできます。
  
 <code php> <code php>
行 120: 行 111:
 \\ \\
  
-==== Set Header ====+==== ヘッダーをセットする ====
  
-You can set a header value with the PSR-7 Response object’s ''withHeader($name, $value)'' method. +PSR-7 レスポンスオブジェクトの ''withHeader($name$value)'' メソッドを使用してヘッダー値を設定できます。
- +
-PSR-7Responseオブジェクトの '' withHeader$ name$ value) ''メソッドを使用してヘッダー値を設定できます。+
  
 <code php> <code php>
行 130: 行 119:
 </code> </code>
  
->**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, and it replaces existing header values already associated with the same header name. +>Responseオブジェクトは不変です。このメソッドは、新しいヘッダー値を持つResponseオブジェクトのコピーを返します。このメソッドは破壊的であり、同じヘッダー名にすでに関連付けられている既存のヘッダー値を置き換えます。
- +
->**リマインダー** +
->Responseオブジェクトは不変です。 このメソッドは、新しいヘッダー値を持つResponseオブジェクトのコピーを返します。 このメソッドは破壊的であり、同じヘッダー名にすでに関連付けられている既存のヘッダー値を置き換えます。+
  
 \\ \\
  
-==== Append Header ====+==== ヘッダーを追加する ====
  
-You can append a header value with the PSR-7 Response object’s ''withAddedHeader($name, $value)'' method. +PSR-7 Responseオブジェクトの ''withAddedHeader($name、$value)'' メソッドを使用してヘッダー値を追加できます。
- +
-PSR-7Responseオブジェクトの '' withAddedHeader$ name、$ value) ''メソッドを使用してヘッダー値を追加できます。+
  
 <code php> <code php>
行 148: 行 132:
 </code> </code>
  
->**Reminder** +> **注意** 
->Unlike the withHeader() method, this method appends the new value to the set of values that already exist for the same header name. The Response object is immutable. This method returns a copy of the Response object that has the appended header value. +> withHeader() メソッドとは異なり、このメソッドは、同じヘッダー名として既に存在する値のセットに新しい値を追加します。Response オブジェクトは不変です。このメソッドは、ヘッダー値が追加されたResponseオブジェクトのコピーを返します。
- +
-> **リマインダー** +
-> withHeader()メソッドとは異なり、このメソッドは、同じヘッダー名にすでに存在する値のセットに新しい値を追加します。 Responseオブジェクトは不変です。 このメソッドは、ヘッダー値が追加されたResponseオブジェクトのコピーを返します。+
  
 \\ \\
  
-==== Remove Header ====+==== ヘッダーを削除する ====
  
-You can remove a header with the Response object’s ''withoutHeader($name)'' method. +Responseオブジェクトの ''withoutHeader($name)'' メソッドを使用してヘッダーを削除できます。
- +
-Responseオブジェクトの '' withoutHeader$ name) ''メソッドを使用してヘッダーを削除できます。+
  
 <code php> <code php>
行 167: 行 146:
  
 > **リマインダー** > **リマインダー**
-> Responseオブジェクトは不変です。 このメソッドは、ヘッダー値が追加されたResponseオブジェクトのコピーを返します。+> Responseオブジェクトは不変です。 このメソッドは、ヘッダー値を削除したResponseオブジェクトのコピーを返します。
  
 \\ \\
  
-===== The Response Body =====+===== レスポンスボディ =====
  
-An HTTP response typically has a body.+HTTPレスポンスには通常、ボディがあります。
  
-HTTP応答には通常、本文があります。 +PSR-7 Requestオブジェクトと同様に、PSR-7 Responseオブジェクトはボディを ''Psr\Http\Message\StreamInterface'' のインスタンスとして実装します。PSR-7 Responseオブジェクトの ''getBody()'' メソッドを使用して、HTTPレスポンスボディの ''StreamInterface'' インスタンスを取得できます。''getBody()'' メソッドは、発信HTTPレスポンスの長さが不明であるか、使用可能なメモリに対して大きすぎる場合に適しています。
- +
-Just like the PSR-7 Request object, the PSR-7 Response object implements the body as an instance of ''Psr\Http\Message\StreamInterface''. You can get the HTTP response body ''StreamInterface'' instance with the PSR-7 Response object’s ''getBody()'' method. The ''getBody()'' method is preferable if the outgoing HTTP response length is unknown or too large for available memory. +
- +
-PSR-7 Requestオブジェクトと同様に、PSR-7Responseオブジェクトは本体Psr \ Http \ Message \ StreamInterfaceのインスタンスとして実装します。 PSR-7 ResponseオブジェクトのgetBody()」メソッドを使用して、HTTP応答本体StreamInterfaceインスタンスを取得できます。 '' getBody() ''メソッドは、発信HTTP応答の長さが不明であるか、使用可能なメモリに対して大きすぎる場合に適しています。+
  
 <code php> <code php>
行 185: 行 160:
 </code> </code>
  
-The resultant ''Psr\Http\Message\StreamInterface'' instance provides the following methods to read from, iterate, and write to its underlying PHP ''resource''+結果として得られる ''Psr\Http\Message\StreamInterface'' インスタンスは、基礎となるPHPの ''resource'' からの読み取り、反復、および書き込みを行うための次のメソッドを提供します。
- +
-結果の '' Psr \ Http \ Message \ StreamInterface ''インスタンスは、基なるPHPの ''リソース ''からの読み取り、反復、および書き込みを行うための次のメソッドを提供します。+
  
   * getSize()   * getSize()
行 202: 行 175:
   * 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 ''StreamInterface'' instance with its ''write()'' method like this: +ほとんどの場合、PSR-7 Responseオブジェクトに書き込む必要があります。次のように、''write()'' メソッドを使用して ''StreamInterface'' インスタンスにコンテンツを書き込むことができます
- +
-ほとんどの場合、PSR-7Responseオブジェクトに書き込む必要があります。 次のように、write()」メソッドを使用してStreamInterfaceインスタンスにコンテンツを書き込むことができます+
  
 <code php> <code php>
行 211: 行 182:
 </code> </code>
  
-You can also replace the PSR-7 Response object’s body with an entirely new ''StreamInterface'' instance. This is particularly useful when you want to pipe content from a remote destination (e.g. the filesystem or a remote API) into the HTTP response. You can replace the PSR-7 Response object’s body with its ''withBody(StreamInterface $body)'' method. Its argument MUST be an instance of ''Psr\Http\Message\StreamInterface''.+PSR-7 Responseオブジェクトのボディをまったく新しい ''StreamInterface'' インスタンスに置き換えることもできます。
  
-PSR-7Responseオブジェクトの本体をまったく新しい「StreamInterface」インスタンスに置き換えることもできます。 これは、コンテンツをリモートの宛先(ファイルシステムやリモートAPIなど)からHTTP応答にパイプ処理する場合に特に便利です。 PSR-7Responseオブジェクトの本体その '' withBodyStreamInterface $ body) ''メソッド置き換えることができます。 その引数は、 '' Psr \ Http \ Message \ StreamInterface ''のインスタンスでなければなりません。+これは、コンテンツをリモートの宛先(例えば、ファイルシステムやリモートAPIなど)からHTTPレスポンスにパイプ処理する場合に特に便利です。PSR-7 Responseオブジェクトのボディを ''withBody(StreamInterface $body)'' メソッドを使って置き換えることができます。 その引数は、''Psr\Http\Message\StreamInterface'' のインスタンスでなければなりません( ''MUST'' )
  
 <code php> <code php>
行 222: 行 193:
 </code> </code>
  
->**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 ===== +===== JSONを返す =====
- +
-In it’s simplest form, JSON data can be returned with a default 200 HTTP status code.+
  
-最も単純な形式では、JSONデータはデフォルトの200HTTPステータスコード返すことができます。+最も単純な形式では、JSONデータはデフォルトの 200 HTTPステータスコードを使用して返すことができます。
  
 <code php> <code php>
行 244: 行 210:
           ->withHeader('Content-Type', 'application/json');           ->withHeader('Content-Type', 'application/json');
 </code> </code>
- 
-We can also return JSON data with a custom HTTP status code. 
  
 カスタムHTTPステータスコードを使用してJSONデータを返すこともできます。 カスタムHTTPステータスコードを使用してJSONデータを返すこともできます。
行 259: 行 223:
 </code> </code>
  
->**Reminder** 
 >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, and it replaces the existing Content-Type header. >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, and it replaces the existing Content-Type header.
  
-> **リマインダー** +> **注意** 
-> Responseオブジェクトは不変です。 このメソッドは、新しいContent-Typeヘッダーを持つResponseオブジェクトのコピーを返します。 このメソッドは破壊的であり、既存のContent-Typeヘッダーを置き換えます。+> Responseオブジェクトは不変です。このメソッドは、新しいContent-Typeヘッダーを持つResponseオブジェクトのコピーを返します。このメソッドは破壊的であり、既存のContent-Typeヘッダーを置き換えます。
  
 \\ \\
  
-===== Returning a Redirect ===== +===== リダイレクトを返す =====
- +
-You can redirect the HTTP client by using the Location header.+
  
 Locationヘッダーを使用して、HTTPクライアントをリダイレクトできます。 Locationヘッダーを使用して、HTTPクライアントをリダイレクトできます。
slim/4/response.1601973019.txt.gz · 最終更新: 2020/10/06 17:30 by y2sunlight