このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
psr:psr7 [2020/06/16 20:53] y2sunlight [1.4 リクエストターゲットとリクエストURI] |
psr:psr7 [2020/09/01 11:53] (現在) tanaka [PSR-7: HTTP message interfaces] |
||
---|---|---|---|
行 3: | 行 3: | ||
--- // | --- // | ||
- | 本章は、若干の補足を加筆してはいるものの単に[[https:// | + | 本章は、若干の補足を加筆してはいるものの単に[[https:// |
+ | |||
+ | 関連記事 | ||
- | ==== 目次 ==== | ||
* [[psr: | * [[psr: | ||
* [[psr: | * [[psr: | ||
行 14: | 行 15: | ||
* PSR-7: HTTP Message Interface - HTTPメッセージインターフェイス | * PSR-7: HTTP Message Interface - HTTPメッセージインターフェイス | ||
* [[psr: | * [[psr: | ||
+ | * [[psr: | ||
+ | * [[psr: | ||
+ | * [[psr: | ||
+ | * [[psr: | ||
+ | * [[psr: | ||
+ | * [[psr: | ||
+ | * [[psr: | ||
+ | * [[psr: | ||
----- | ----- | ||
行 193: | 行 202: | ||
たとえば、ユーザーがサーバーに asterisk-form のリクエストを行う場合があります: | たとえば、ユーザーがサーバーに asterisk-form のリクエストを行う場合があります: | ||
- | <code php> | + | <code php> |
$request = $request | $request = $request | ||
-> | -> | ||
-> | -> | ||
-> | -> | ||
- | |||
</ | </ | ||
行 222: | 行 230: | ||
==== 1.5 サーバーサイドリクエスト ==== | ==== 1.5 サーバーサイドリクエスト ==== | ||
- | RequestInterface provides the general representation of an HTTP request message. However, server-side requests need additional treatment, due to the nature of the server-side environment. Server-side processing needs to take into account Common Gateway Interface (CGI), and, more specifically, | ||
- | RequestInterfaceは、HTTPリクエストメッセージの一般的な表現を提供します。 ただし、サーバー側の環境の性質上、サーバー側のリクエストには追加の処理が必要です。 サーバー側の処理では、Common Gateway Interface(CGI)、PHP(さらに具体的には、サーバーAPI(SAPI)を介したCGIの抽象化)と拡張を考慮する必要があります。 PHPは、次のようなスーパーグローバル変数を介して入力のマーシャリングを簡素化しました。 | + | '' |
- | * $_COOKIE, which deserializes and provides simplified access to HTTP cookies. | + | |
- | * $_GET, which deserializes and provides simplified access to query string arguments. | + | |
- | * $_POST, which deserializes and provides simplified access for urlencoded parameters submitted via HTTP POST; generically, | + | |
- | * $_FILES, which provides serialized metadata around file uploads. | + | |
- | * $_SERVER, which provides access to CGI/ | + | |
- | * $_COOKIE: HTTPCookieへのデシリアル化と簡略化されたアクセスを提供します。 | + | '' |
- | * $_GET: クエリ文字列引数へのデシリアル化と簡略化されたアクセスを提供します。 | + | |
- | * $_POST: HTTP POST経由で送信されたURLエンコードされたパラメーターへのデシリアル化と簡略化されたアクセスを提供します。 | + | |
- | * $_FILES: ファイルのアップロードに関するシリアル化されたメタデータを提供します。 | + | |
- | * $_SERVER: CGI / SAPI環境変数へのアクセスを提供します。これには、通常、リクエストメソッド、リクエストスキーム、リクエストURI、およびヘッダーが含まれます。 | + | |
- | ServerRequestInterface extends RequestInterface to provide an abstraction around these various superglobals. This practice helps reduce coupling to the superglobals by consumers, and encourages and promotes the ability to test request consumers. | + | サーバーリクエストは、「attributes」という1つの追加プロパティを提供して、コンシューマーがアプリケーション固有のルール(パスマッチング、スキームマッチング、ホストマッチングなど)に対してリクエストを内省(introspect)、分解(decompose)、および照合(match)できるようにします。従って、サーバーリクエストは、複数のリクエストコンシューマー間のメッセージングを提供することもできます。 |
- | + | ||
- | ServerRequestInterfaceはRequestInterfaceを拡張して、これらのさまざまなスーパーグローバル変数に関する抽象化を提供します。 このプラクティスは、コンシューマーによるスーパーグローバル変数への結合を減らすのに役立ち、リクエストコンシューマーをテストする機能を奨励および促進します。 | + | |
- | + | ||
- | The server request provides one additional property, “attributes”, | + | |
- | + | ||
- | サーバーリクエストは、「attributes」という1つの追加プロパティを提供して、コンシューマーがアプリケーション固有のルール(パスマッチング、スキームマッチング、ホストマッチングなど)に対してリクエストを内省(introspect)、分解(decompose)、および照合(match)できるようにします。 | + | |
\\ | \\ | ||
行 250: | 行 247: | ||
==== 1.6 アップロードファイル ==== | ==== 1.6 アップロードファイル ==== | ||
- | ServerRequestInterface | + | '' |
- | + | ||
- | ServerRequestInterfaceは、正規化された構造でアップロードファイルのツリーを取得するメソッドを規定します。そのツリーの各リーフにはUploadedFileInterfaceのインスタンスがあります。 | + | |
- | + | ||
- | The $_FILES superglobal has some well-known problems when dealing with arrays of file inputs. As an example, if you have a form that submits an array of files — e.g., the input name “files”, | + | |
- | $_FILESスーパーグローバル変数には、ファイル入力の配列を処理するときによく知られた問題がいくつかあります。 例として、ファイルの配列を送信するフォームがある場合(例えば入力名を「files」とする)、files[0] とfiles[1] を送信すると、PHPはこれを次のように表します: | + | '' |
<code php> | <code php> | ||
行 273: | 行 266: | ||
) | ) | ||
</ | </ | ||
- | |||
- | instead of the expected: | ||
期待されるのは次のようです: | 期待されるのは次のようです: | ||
行 294: | 行 285: | ||
) | ) | ||
</ | </ | ||
- | |||
- | The result is that consumers need to know this language implementation detail, and write code for gathering the data for a given upload. | ||
その結果、コンシューマーはこの言語実装(PHP)の詳細を知って、与えられたアップロードのデータを収集するためのコードを書く必要があります。 | その結果、コンシューマーはこの言語実装(PHP)の詳細を知って、与えられたアップロードのデータを収集するためのコードを書く必要があります。 | ||
- | Additionally, | + | さらに、ファイルのアップロードが発生したときに '' |
- | さらに、ファイルのアップロードが発生したときに$_FILESが入力されない状況があります: | + | * HTTPメソッドが |
+ | * ユニットテストの時 | ||
+ | * [[https:// | ||
- | * When the HTTP method is not POST. | + | そのような場合、データを別の方法でシード(特別な処理)する必要があります。例としては: |
- | * When unit testing. | + | |
- | * When operating under a non-SAPI environment, | + | |
- | + | ||
- | * HTTPメソッドがPOSTでない場合 | + | |
- | * ユニットテスト時 | + | |
- | * ReactPHPのように非SAPI環境下で操作する場合 | + | |
- | + | ||
- | In such cases, the data will need to be seeded differently. As examples: | + | |
- | + | ||
- | そのような場合、データを別の方法でシード(特別な処理)する必要があります。 例としては: | + | |
- | + | ||
- | * A process might parse the message body to discover the file uploads. In such cases, the implementation may choose not to write the file uploads to the file system, but instead wrap them in a stream in order to reduce memory, I/O, and storage overhead. | + | |
* プロセスがメッセージ本文を解析して、ファイルのアップロードを検出する場合があります。 このような場合、実装では、ファイルアップロードをファイルシステムに書き込まず、代わりにストリームにラップして、メモリ、I/ | * プロセスがメッセージ本文を解析して、ファイルのアップロードを検出する場合があります。 このような場合、実装では、ファイルアップロードをファイルシステムに書き込まず、代わりにストリームにラップして、メモリ、I/ | ||
- | |||
- | * In unit testing scenarios, developers need to be able to stub and/or mock the file upload metadata in order to validate and verify different scenarios. | ||
* ユニットテストのシナリオでは、さまざまなシナリオを検証および検査するために、開発者はファイルアップロードのメタデータをスタブ化および/ | * ユニットテストのシナリオでは、さまざまなシナリオを検証および検査するために、開発者はファイルアップロードのメタデータをスタブ化および/ | ||
- | getUploadedFiles() | + | '' |
- | + | ||
- | getUploadedFiles() | + | |
- | + | ||
- | * Aggregate all information for a given file upload, and use it to populate a Psr\Http\Message\UploadedFileInterface instance. | + | |
- | * Re-create the submitted tree structure, with each leaf being the appropriate Psr\Http\Message\UploadedFileInterface instance for the given location in the tree. | + | |
- | * 与えられたファイルアップロードのすべての情報を集約し、それを使用してPsr\Http\Message\UploadedFileInterfaceインスタンスに入力します。 | + | * 与えられたファイルアップロードのすべての情報を集約し、それを使用して |
- | * 送信されたツリー構造を再作成します。その時各リーフは、ツリー内の与えられた場所に対して適切なPsr\Http\Message\UploadedFileInterfaceのインスタンスになります。 | + | |
- | The tree structure referenced should mimic the naming structure in which files were submitted. | + | * 送信されたツリー構造を再作成します。その時各リーフは、ツリー内の与えられた場所に対して適切な'' |
参照されるツリー構造は、ファイルが送信されたときのネーミング構造を模倣する必要があります。 | 参照されるツリー構造は、ファイルが送信されたときのネーミング構造を模倣する必要があります。 | ||
- | |||
- | In the simplest example, this might be a single named form element submitted as: | ||
最も単純な例では、これは次のように送信された単一の名前付きフォーム要素でしょう: | 最も単純な例では、これは次のように送信された単一の名前付きフォーム要素でしょう: | ||
行 345: | 行 314: | ||
</ | </ | ||
- | In this case, the structure in $_FILES would look like: | + | この場合、'' |
- | + | ||
- | この場合、$_FILESの構造は次のようになります: | + | |
<code php> | <code php> | ||
行 361: | 行 328: | ||
</ | </ | ||
- | The normalized form returned by getUploadedFiles() would be: | + | '' |
- | + | ||
- | getUploadedFiles() によって返される正規化された形式は次のようになります: | + | |
<code php> | <code php> | ||
行 370: | 行 335: | ||
) | ) | ||
</ | </ | ||
- | |||
- | In the case of an input using array notation for the name: | ||
名前に配列表記を使用した入力の場合: | 名前に配列表記を使用した入力の場合: | ||
行 378: | 行 341: | ||
<input type=" | <input type=" | ||
</ | </ | ||
- | |||
- | $_FILES ends up looking like this: | ||
$_FILESは次のようになります: | $_FILESは次のようになります: | ||
行 416: | 行 377: | ||
</ | </ | ||
- | And the corresponding tree returned by getUploadedFiles() should be: | + | そして、'' |
- | + | ||
- | そして、getUploadedFiles() によって返される対応するツリーは次のようになります: | + | |
<code php> | <code php> | ||
行 429: | 行 388: | ||
) | ) | ||
</ | </ | ||
- | |||
- | In some cases, you may specify an array of files: | ||
場合によっては、ファイルの配列を指定できます: | 場合によっては、ファイルの配列を指定できます: | ||
行 438: | 行 395: | ||
Upload an avatar: <input type=" | Upload an avatar: <input type=" | ||
</ | </ | ||
- | |||
- | (As an example, JavaScript controls might spawn additional file upload inputs to allow uploading multiple files at once.) | ||
(例として、JavaScriptコントロールは、複数のファイルを一度にアップロードできるように、追加のファイルアップロード入力を生成する場合があります。) | (例として、JavaScriptコントロールは、複数のファイルを一度にアップロードできるように、追加のファイルアップロード入力を生成する場合があります。) | ||
- | In such a case, the specification implementation must aggregate all information related to the file at the given index. The reason is because $_FILES deviates from its normal structure in such cases: | + | このような場合、仕様の実装では、指定されたインデックスにあるファイルに関連するすべての情報を集約する必要があります。その理由は、'' |
- | + | ||
- | このような場合、仕様の実装では、指定されたインデックスにあるファイルに関連するすべての情報を集約する必要があります。 その理由は、$_FILESは通常の構造から逸脱しているためです。それは、次のような場合です: | + | |
<code php> | <code php> | ||
行 499: | 行 452: | ||
</ | </ | ||
- | The above $_FILES array would correspond to the following structure as returned by getUploadedFiles(): | + | 上記の |
- | + | ||
- | 上記の$_FILES配列は、getUploadedFiles() によって返される次の構造に対応します: | + | |
<code php> | <code php> | ||
行 516: | 行 467: | ||
) | ) | ||
</ | </ | ||
- | |||
- | Consumers would access index 1 of the nested array using: | ||
コンシューマーは、ネストされた配列のインデックス1に次のようにしてアクセスします: | コンシューマーは、ネストされた配列のインデックス1に次のようにしてアクセスします: | ||
行 525: | 行 474: | ||
</ | </ | ||
- | Because the uploaded files data is derivative (derived from $_FILES or the request body), a mutator method, withUploadedFiles(), | + | アップロードされたファイルデータは派生型('' |
- | + | ||
- | アップロードされたファイルデータは派生物($_FILESまたはリクエストボディからの派生)であるため、インターフェイスにはミューテーターメソッド(mutator method) としてwithUploadedFiles() も存在し、正規化を別のプロセスに委譲できます。 | + | |
- | + | ||
- | In the case of the original examples, consumption resembles the following: | + | |
冒頭の例の場合、使用方法は次のようになります: | 冒頭の例の場合、使用方法は次のようになります: | ||
行 546: | 行 491: | ||
</ | </ | ||
- | This proposal also recognizes that implementations may operate in non-SAPI environments. As such, UploadedFileInterface | + | この提案は、実装が非SAPI環境で動作する可能性があることも認識しています。従って、'' |
- | この提案は、実装が非SAPI環境で動作する可能性があることも認識しています。 したがって、UploadedFileInterfaceは、環境に関係なく操作が確実に機能するためのメソッドを提供します。 特に: | + | |
- | + | ||
- | + | ||
- | * moveTo($targetPath) is provided as a safe and recommended alternative to calling move_uploaded_file() directly on the temporary upload file. Implementations will detect the correct operation to use based on environment. | + | |
- | + | ||
- | | + | |
- | + | ||
- | * getStream() will return a StreamInterface instance. In non-SAPI environments, | + | |
- | + | ||
- | * getStream() はStreamInterfaceインスタンスを返します。 非SAPIの環境下では、個々のアップロードファイルを解析して直接ファイルにではなくphp:// | + | |
- | As examples: | + | * '' |
例として: | 例として: | ||
行 566: | 行 502: | ||
// Move a file to an upload directory | // Move a file to an upload directory | ||
// ファイルをアップロードディレクトリに移動する | // ファイルをアップロードディレクトリに移動する | ||
+ | |||
$filename = sprintf( | $filename = sprintf( | ||
' | ' | ||
行 577: | 行 514: | ||
// Psr7StreamWrapper is a class that will decorate a StreamInterface as a PHP | // Psr7StreamWrapper is a class that will decorate a StreamInterface as a PHP | ||
// StreamWrapper. | // StreamWrapper. | ||
- | // ファイルをAmazon S3にストリーミングします。 | + | // |
- | // $s3wrapperがS3に書き込むPHPストリームであり、Psr7StreamWrapperがStreamInterfaceを | + | // ファイルを Amazon S3 にストリーミングします。 |
- | // PHP StreamWrapperとして装飾するクラスであると想定します。 | + | // $s3wrapper が S3 に書き込むPHPストリームであり、 |
+ | // Psr7StreamWrapper が StreamInterface を | ||
+ | // PHP StreamWrapper として装飾するクラスであると仮定します。 | ||
$stream = new Psr7StreamWrapper($file1-> | $stream = new Psr7StreamWrapper($file1-> | ||
stream_copy_to_stream($stream, | stream_copy_to_stream($stream, |