# RequestCollector

> Captures PSR-7 request and response state in the canonical Request panel shape.

[API reference](/docs/api/index.md) › [Yii3 Debug](/docs/extensions/yii3-debug/reference/index.md) › [Collector](/docs/extensions/yii3-debug/reference/collector/index.md) › RequestCollector

| final class | `Yii3\Debug\Collector\RequestCollector` |
| --- | --- |
| Implements | [`CollectorInterface`](https://github.com/php-forge/debug/blob/e4fbb8c3238a4d011ceef5e0094344abd50deaf4/src/CollectorInterface.php), [`RequestObserverInterface`](/docs/extensions/yii3-debug/reference/collector/requestobserverinterface/index.md) |
| Source | [`src/Collector/RequestCollector.php`](https://github.com/yii3/debug/blob/main/src/Collector/RequestCollector.php#L34) |

Captures PSR-7 request and response state in the canonical Request panel shape.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Creates a request collector with routing context and the capture policy. | `RequestCollector` |
| [`capture()`](#method-capture) | Encodes the captured request and response into the Request panel payload. | `RequestCollector` |
| [`collectRequest()`](#method-collectRequest) | Records the request line, headers, body, and matched route, applying the capture policy. | `RequestCollector` |
| [`collectResponse()`](#method-collectResponse) | Records the response status and headers, applying the capture policy. | `RequestCollector` |
| [`id()`](#method-id) | Returns the stable identifier of this collector. | `RequestCollector` |
| [`shutdown()`](#method-shutdown) | Stops capturing and clears the data accumulated for the request. | `RequestCollector` |
| [`startup()`](#method-startup) | Starts capturing, discarding anything left from a previous request. | `RequestCollector` |

## Method details

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

```php
public function __construct(Yiisoft\Router\CurrentRoute|null $currentRoute = null, Yiisoft\Router\RouteCollectionInterface|null $routes = null, PHPForge\Debug\Capture\CapturePolicy $capturePolicy = new \PHPForge\Debug\Capture\CapturePolicy())
```

Creates a request collector with routing context and the capture policy.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$currentRoute` | `Yiisoft\Router\CurrentRoute\|null` | Route matched for the request, or `null` when routing did not run. |
| `$routes` | `Yiisoft\Router\RouteCollectionInterface\|null` | Live route collection, or `null` when it cannot be resolved. |
| `$capturePolicy` | [`CapturePolicy`](https://github.com/php-forge/debug-core/blob/b70459f033a1d7a1c5edf7951029aad2061986dd/src/Capture/CapturePolicy.php) | Policy deciding which values are persisted and which are redacted. |

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

```php
public function capture(): array<string, mixed>|null
```

Encodes the captured request and response into the Request panel payload.

When the handler never returned a response, because the script ended inside it through `exit`, `dd()`, or a
fatal error, the response half carries the matched route and the status code PHP is about to send, without
headers, since no PSR-7 response exists.

**Returns**

`array<string, mixed>|null`: Encoded Request panel payload; `null` when the request was not observed.

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

```php
public function collectRequest(Psr\Http\Message\ServerRequestInterface $request): void
```

Records the request line, headers, body, and matched route, applying the capture policy.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$request` | `Psr\Http\Message\ServerRequestInterface` | Request reaching the debugger middleware. |

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

```php
public function collectResponse(Psr\Http\Message\ResponseInterface $response): void
```

Records the response status and headers, applying the capture policy.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$response` | `Psr\Http\Message\ResponseInterface` | Response produced for the captured request. |

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

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

Returns the stable identifier of this collector.

**Returns**

`string`: Stable ID pairing this collector with its panel.

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

```php
public function shutdown(): void
```

Stops capturing and clears the data accumulated for the request.

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

```php
public function startup(): void
```

Starts capturing, discarding anything left from a previous request.