# ResponseAdapter

> Adapts Yii responses to PSR-7 responses.

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

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

Adapts Yii responses to PSR-7 responses.

`ResponseInterface` PSR-7 response contract.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Creates a new instance of the [`ResponseAdapter`](/docs/extensions/psr-bridge/reference/adapter/responseadapter/index.md) class. | `ResponseAdapter` |
| [`toPsr7()`](#method-toPsr7) | Converts the Yii Response instance to a PSR-7 ResponseInterface. | `ResponseAdapter` |

## Method details

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

```php
public function __construct(yii2\extensions\psrbridge\http\Response $psrResponse, Psr\Http\Message\ResponseFactoryInterface $responseFactory, Psr\Http\Message\StreamFactoryInterface $streamFactory, yii\base\Security $security)
```

Creates a new instance of the [`ResponseAdapter`](/docs/extensions/psr-bridge/reference/adapter/responseadapter/index.md) class.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$psrResponse` | [`yii2\extensions\psrbridge\http\Response`](/docs/extensions/psr-bridge/reference/http/response/index.md) | Yii Response instance to adapt. |
| `$responseFactory` | `Psr\Http\Message\ResponseFactoryInterface` | PSR-7 ResponseFactoryInterface instance for response creation. |
| `$streamFactory` | `Psr\Http\Message\StreamFactoryInterface` | PSR-7 StreamFactoryInterface instance for body stream creation. |
| `$security` | [`yii\base\Security`](/docs/api/yii/base/security/index.md) | Security component used to sign outgoing cookie values. |

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

```php
public function toPsr7(): Psr\Http\Message\ResponseInterface
```

Converts the Yii Response instance to a PSR-7 ResponseInterface.

Creates a new PSR-7 ResponseInterface using the configured response and stream factories, copying status code,
status text, headers, cookies, and body content from the Yii Response component.
- All headers are transferred to the PSR-7 ResponseInterface, with multiple values preserved.
- Cookies are formatted and added as separate 'Set-Cookie' headers.
- File streaming is supported with HTTP range handling for efficient large file downloads.
- Response body is created from the Yii Response content or stream using the PSR-7 StreamFactoryInterface.

**Returns**

`Psr\Http\Message\ResponseInterface`: PSR-7 ResponseInterface instance with headers, cookies, and body content.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if the configuration is invalid or incomplete.