Abstract Class Yiisoft\DataResponse\Middleware\AbstractDataResponseMiddleware
Abstract middleware class that applies a formatter to Yiisoft\DataResponse\DataStream\DataStream responses and sets appropriate response headers.
The middleware only formats responses whose body is a Yiisoft\DataResponse\DataStream\DataStream without a formatter set.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\DataResponse\Middleware\AbstractDataResponseMiddleware | |
| process() | Yiisoft\DataResponse\Middleware\AbstractDataResponseMiddleware |
Method Details
| public __construct( Yiisoft\DataResponse\Formatter\FormatterInterface $formatter ): mixed | ||
| $formatter | Yiisoft\DataResponse\Formatter\FormatterInterface |
The formatter to apply to the response. |
public function __construct(
private readonly FormatterInterface $formatter,
) {}
| public process( \Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Server\RequestHandlerInterface $handler ): \Psr\Http\Message\ResponseInterface | ||
| $request | \Psr\Http\Message\ServerRequestInterface | |
| $handler | \Psr\Http\Server\RequestHandlerInterface | |
final public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$response = $handler->handle($request);
$body = $response->getBody();
if (!$body instanceof DataStream || $body->hasFormatter()) {
return $response;
}
$body->changeFormatter($this->formatter);
return $this->formatter->formatResponse($response);
}
Signup or Login in order to comment.