Naify\Psr\Message\ServerRequest クラス
implements
Naify\Psr\Contract\ServerRequestInterface, Naify\Psr\Contract\MessageInterface, Naify\Psr\Contract\RequestInterface
PSR-7 サーバーリクエスト実装 — ServerRequestInterface
HTTP リクエストを表現する不変オブジェクト。 $_SERVER, $_GET, $_POST, $_COOKIE, $_FILES から生成される。
使用例
// ServerRequestFactory::fromGlobals() で生成するのが通常の使い方
$request = ServerRequestFactory::fromGlobals();
echo $request->getMethod(); // "GET"
echo $request->getUri()->getPath(); // "/some/path"
$params = $request->getQueryParams(); // $_GET 相当
設計意図
readonly 化は各 with メソッドの PHP 8.5 clone with 対応と初期化経路の確認を前提とする。
PHP 8.3 の再初期化は __clone() 内だけであり、clone 後の代入には使えない。
プロパティ
| 名前 | 型 | 説明 |
|---|---|---|
private
$method
|
string |
HTTP メソッド |
private
$uri
|
UriInterface |
リクエストURI |
private
$requestTarget
|
string |
リクエストターゲット |
private
$serverParams
|
array |
サーバーパラメータ($_SERVER) |
private
$cookieParams
|
array |
クッキー($_COOKIE) |
private
$queryParams
|
array |
クエリパラメータ($_GET) |
private
$uploadedFiles
|
array |
アップロードファイル情報 |
private
$parsedBody
|
object|array|null |
パース済みボディ($_POST等) |
private
$attributes
|
array |
リクエスト属性 |
メソッド
| メソッド | 説明 |
|---|---|
public
__construct(string $method = GET, ?UriInterface $uri, array $headers = '...', ?StreamInterface $body, string $version = 1.1, array $serverParams = '...', array $cookieParams = '...', array $queryParams = '...', object|array|null $parsedBody, array $uploadedFiles = '...'):
|
スーパーグローバル由来の情報を保持する不変 request を初期化する |
public
getRequestTarget(): string
|
request-target を返す |
public
withRequestTarget(string $requestTarget): static
|
request-target を差し替えた新しいインスタンスを返す 8.5 → clone with |
public
getMethod(): string
|
HTTP メソッドを返す |
public
withMethod(string $method): static
|
HTTP メソッドを差し替えた新しいインスタンスを返す 8.5 → clone with |
public
getUri(): UriInterface
|
現在のリクエスト URI を返す |
public
withUri(UriInterface $uri, bool $preserveHost = false): static
|
URI を差し替えた新しいインスタンスを返す 8.5 → clone with |
public
getServerParams(): array
|
サーバーパラメータを返す |
public
getCookieParams(): array
|
クッキーパラメータを返す |
public
withCookieParams(array $cookies): static
|
クッキーパラメータを差し替えた新しいインスタンスを返す 8.5 → clone with |
public
getQueryParams(): array
|
クエリパラメータを返す |
public
withQueryParams(array $query): static
|
クエリパラメータを差し替えた新しいインスタンスを返す 8.5 → clone with |
public
getUploadedFiles(): array
|
アップロードファイル情報を返す |
public
withUploadedFiles(array $uploadedFiles): static
|
アップロードファイル情報を差し替えた新しいインスタンスを返す 8.5 → clone with |
public
getParsedBody(): object|array|null
|
パース済みボディを返す |
public
withParsedBody(mixed $data): static
|
パース済みボディを差し替えた新しいインスタンスを返す 8.5 → clone with |
public
getAttributes(): array
|
リクエスト属性をすべて返す |
public
getAttribute(string $name, mixed $default): mixed
|
指定したリクエスト属性を返す |
public
withAttribute(string $name, mixed $value): static
|
リクエスト属性を追加した新しいインスタンスを返す 8.5 → clone with |
public
withoutAttribute(string $name): static
|
指定したリクエスト属性を除外した新しいインスタンスを返す 8.5 → clone with |
継承プロパティ
| 名前 | 型 | 説明 | 継承元 |
|---|---|---|---|
private
$headers
|
array |
ヘッダー(オリジナルケース) | MessageTrait |
private
$headerNames
|
array |
小文字ヘッダー名 → オリジナルケースのマップ | MessageTrait |
private
$body
|
StreamInterface |
メッセージボディ | MessageTrait |
private
$protocolVersion
|
string |
HTTP プロトコルバージョン | MessageTrait |
継承メソッド
| メソッド | 説明 | 継承元 |
|---|---|---|
private
setHeaders(): void
|
ヘッダー配列を正規化して格納する(コンストラクタ用) | MessageTrait |
public
getProtocolVersion(): string
|
現在の HTTP プロトコルバージョンを返す | MessageTrait |
public
withProtocolVersion(): static
|
プロトコルバージョンを変更した新しいインスタンスを返す | MessageTrait |
public
getHeaders(): array
|
保持しているヘッダーをすべて返す | MessageTrait |
public
hasHeader(): bool
|
指定したヘッダーの存在を判定する | MessageTrait |
public
getHeader(): array
|
指定したヘッダーの値一覧を返す | MessageTrait |
public
getHeaderLine(): string
|
指定したヘッダーを 1 行文字列にして返す | MessageTrait |
public
withHeader(): static
|
ヘッダーを設定する(既存の同名ヘッダーを置換) | MessageTrait |
public
withAddedHeader(): static
|
ヘッダーを追加する(既存の同名ヘッダーに値を追加) | MessageTrait |
public
withoutHeader(): static
|
ヘッダーを削除する | MessageTrait |
public
getBody(): StreamInterface
|
メッセージボディのストリームを返す | MessageTrait |
public
withBody(): static
|
メッセージボディを変更した新しいインスタンスを返す | MessageTrait |