# ServerRequestAdapter

> Adapts PSR-7 server requests to Yii request expectations.

[API reference](/docs/api/index.md) › [PSR bridge](/docs/extensions/psr-bridge/reference/index.md) › [adapter](/docs/extensions/psr-bridge/reference/adapter/index.md) › ServerRequestAdapter

| final class | `yii2\extensions\psrbridge\adapter\ServerRequestAdapter` |
| --- | --- |
| Source | [`src/adapter/ServerRequestAdapter.php`](https://github.com/yii2-extensions/psr-bridge/blob/main/src/adapter/ServerRequestAdapter.php#L28) |

Adapts PSR-7 server requests to Yii request expectations.

`ServerRequestInterface` PSR-7 server request contract.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-psrRequest"></span>`$psrRequest` readonly | `Psr\Http\Message\ServerRequestInterface` | Adapted PSR-7 ServerRequestInterface instance. | `ServerRequestAdapter` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Adapts a PSR-7 request and parses its body when a matching parser is configured. | `ServerRequestAdapter` |
| [`getBodyParams()`](#method-getBodyParams) | Returns the parsed body, removing the method override key from array bodies when it is non-`null`. | `ServerRequestAdapter` |
| [`getCookies()`](#method-getCookies) | Returns PSR cookies with optional Yii signature and encoded-name validation. | `ServerRequestAdapter` |
| [`getHeaders()`](#method-getHeaders) | Retrieves all HTTP headers from the PSR-7 ServerRequestInterface as a Yii HeaderCollection instance. | `ServerRequestAdapter` |
| [`getMethod()`](#method-getMethod) | Retrieves the HTTP method for the current request, supporting method override via body or header. | `ServerRequestAdapter` |
| [`getParsedBody()`](#method-getParsedBody) | Retrieves the parsed body parameters from the PSR-7 ServerRequestInterface. | `ServerRequestAdapter` |
| [`getQueryParams()`](#method-getQueryParams) | Returns query parameters from the attached PSR request. | `ServerRequestAdapter` |
| [`getQueryString()`](#method-getQueryString) | Retrieves the raw query string from the PSR-7 ServerRequestInterface URI. | `ServerRequestAdapter` |
| [`getRawBody()`](#method-getRawBody) | Retrieves the raw body content from the PSR-7 ServerRequestInterface stream. | `ServerRequestAdapter` |
| [`getServerParams()`](#method-getServerParams) | Retrieves server parameters from the PSR-7 ServerRequestInterface. | `ServerRequestAdapter` |
| [`getUploadedFiles()`](#method-getUploadedFiles) | Retrieves uploaded files from the PSR-7 ServerRequestInterface. | `ServerRequestAdapter` |
| [`getUrl()`](#method-getUrl) | Retrieves the request URL path and query string from the PSR-7 URI. | `ServerRequestAdapter` |

## Method details

### __construct() <span id="method-__construct"></span>

```php
public function __construct(Psr\Http\Message\ServerRequestInterface $psrRequest, array<string, class-string<object>|array{class?: class-string<object>, __class?: class-string<object>}|callable(): object> $parsers = [])
```

Adapts a PSR-7 request and parses its body when a matching parser is configured.

Requests that already contain a parsed body are left unchanged.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$psrRequest` | `Psr\Http\Message\ServerRequestInterface` | Request to adapt. |
| `$parsers` | `array<string, class-string<object>\|array{class?: class-string<object>, __class?: class-string<object>}\|callable(): object>` | Content-Type parser definitions. |

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if a configured parser does not implement RequestParserInterface.

### getBodyParams() <span id="method-getBodyParams"></span>

```php
public function getBodyParams(string $methodParam): mixed[]|object
```

Returns the parsed body, removing the method override key from array bodies when it is non-`null`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$methodParam` | `string` | Method override key to exclude. |

**Returns**

`mixed[]|object`: Body parameters, or `[]` when no parsed body exists.

### getCookies() <span id="method-getCookies"></span>

```php
public function getCookies(bool $enableValidation, string $validationKey = ''): yii\web\Cookie[]
```

Returns PSR cookies with optional Yii signature and encoded-name validation.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$enableValidation` | `bool` | Whether to require signed Yii cookie values. |
| `$validationKey` | `string` | Secret key, required when validation is enabled. |

**Returns**

[`yii\web\Cookie`](/docs/api/yii/web/cookie/index.md)`[]`: Accepted cookies, keyed by cookie name.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): when validation is enabled without a key.

### getHeaders() <span id="method-getHeaders"></span>

```php
public function getHeaders(): yii\web\HeaderCollection
```

Retrieves all HTTP headers from the PSR-7 ServerRequestInterface as a Yii HeaderCollection instance.

Iterates over each header in the PSR-7 ServerRequestInterface and adds it to a new [`HeaderCollection`](/docs/api/yii/web/headercollection/index.md)
instance, concatenating multiple values with a comma and space, as expected by Yii.

**Returns**

[`yii\web\HeaderCollection`](/docs/api/yii/web/headercollection/index.md): Collection of HTTP headers from the PSR-7 ServerRequestInterface.

### getMethod() <span id="method-getMethod"></span>

```php
public function getMethod(string $methodParam = '_method'): string
```

Retrieves the HTTP method for the current request, supporting method override via body or header.

Determines the HTTP method by checking for an override parameter in the parsed body (such as `_method`) or the
X-Http-Method-Override header.
- If a valid override is found and is not one of GET, HEAD, or OPTIONS, it is returned in uppercase.
- Otherwise, the original method from the PSR-7 ServerRequestInterface is returned.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$methodParam` | `string` | Name of the HTTP method override parameter to check in the body (default: `_method`). |

**Returns**

`string`: Resolved HTTP method for the request.

### getParsedBody() <span id="method-getParsedBody"></span>

```php
public function getParsedBody(): mixed[]|object|null
```

Retrieves the parsed body parameters from the PSR-7 ServerRequestInterface.

**Returns**

`mixed[]|object|null`: Parsed body parameters, or `null` if not present.

### getQueryParams() <span id="method-getQueryParams"></span>

```php
public function getQueryParams(): mixed[]
```

Returns query parameters from the attached PSR request.

**Returns**

`mixed[]`: Query parameters.

### getQueryString() <span id="method-getQueryString"></span>

```php
public function getQueryString(): string
```

Retrieves the raw query string from the PSR-7 ServerRequestInterface URI.

**Returns**

`string`: Raw query string from the request URI, or an empty string if not present.

### getRawBody() <span id="method-getRawBody"></span>

```php
public function getRawBody(): string
```

Retrieves the raw body content from the PSR-7 ServerRequestInterface stream.

The stream is rewound before reading (if seekable) to ensure the full content is returned from the beginning.

**Returns**

`string`: Raw body content from the PSR-7 ServerRequestInterface stream.

### getServerParams() <span id="method-getServerParams"></span>

```php
public function getServerParams(): mixed[]
```

Retrieves server parameters from the PSR-7 ServerRequestInterface.

**Returns**

`mixed[]`: Server parameters.

### getUploadedFiles() <span id="method-getUploadedFiles"></span>

```php
public function getUploadedFiles(): mixed[]
```

Retrieves uploaded files from the PSR-7 ServerRequestInterface.

**Returns**

`mixed[]`: Uploaded files.

### getUrl() <span id="method-getUrl"></span>

```php
public function getUrl(): string
```

Retrieves the request URL path and query string from the PSR-7 URI.

This method provides the full URL as seen by the application, excluding the scheme, host, and fragment, ensuring
compatibility with Yii Routing and Request processing expectations.
- If the URI contains a query string, it is appended to the path with a '?' separator.
- If no query string is present, only the path is returned.

**Returns**

`string`: Request URL path and query string, excluding scheme, host, and fragment.