Final Class Yiisoft\DataResponse\Middleware\ContentNegotiator
| Inheritance | Yiisoft\ |
|---|---|
| Implements | Psr\ |
| Deprecated since version | Use {@see \ |
ContentNegotiator supports response format negotiation.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\ |
|
| process() | Yiisoft\ |
|
| withContentFormatters() | Returns a new instance with the specified content formatters. | Yiisoft\ |
Method Details
| public mixed __construct ( array $contentFormatters ) | ||
| $contentFormatters | array |
The array key is the content type, and the value is an instance of
{@see \ |
public function __construct(array $contentFormatters)
{
$this->checkFormatters($contentFormatters);
$this->contentFormatters = $contentFormatters;
}
| public \ | ||
| $request | \ |
|
| $handler | \ |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$response = $handler->handle($request);
if ($response instanceof DataResponse && !$response->hasResponseFormatter()) {
$accepted = $request->getHeader(Header::ACCEPT);
foreach ($accepted as $accept) {
foreach ($this->contentFormatters as $contentType => $formatter) {
if (str_contains($accept, $contentType)) {
return $response->withResponseFormatter($formatter);
}
}
}
}
return $response;
}
Returns a new instance with the specified content formatters.
| public self withContentFormatters ( array $contentFormatters ) | ||
| $contentFormatters | array |
The array key is the content type, and the value is an instance of
{@see \ |
public function withContentFormatters(array $contentFormatters): self
{
$this->checkFormatters($contentFormatters);
$new = clone $this;
$new->contentFormatters = $contentFormatters;
return $new;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.