Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/guide/rest/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,31 @@ For small endpoints, you can write JSON to a PSR-7 response manually as shown in
For APIs, prefer [yiisoft/data-response](https://github.com/yiisoft/data-response), which formats response data and
sets the `Content-Type` header.

When using `DataResponseFactoryInterface`, add `ContentNegotiatorDataResponseMiddleware` to the application middleware
stack. It selects a formatter for data responses from the request's `Accept` header, with JSON as a fallback:

```php
use Yiisoft\DataResponse\Formatter\JsonFormatter;
use Yiisoft\DataResponse\Formatter\XmlFormatter;
use Yiisoft\DataResponse\Middleware\ContentNegotiatorDataResponseMiddleware;
use Yiisoft\ErrorHandler\Middleware\ErrorCatcher;
use Yiisoft\Request\Body\RequestBodyParser;
use Yiisoft\Router\Middleware\Router;

return [
static fn() => new ContentNegotiatorDataResponseMiddleware(
formatters: [
'application/xml' => new XmlFormatter(),
'application/json' => new JsonFormatter(),
],
fallback: new JsonFormatter(),
),
ErrorCatcher::class,
RequestBodyParser::class,
Router::class,
];
```

The API application template keeps response formatting in `src/Api/Shared/ResponseFactory.php`:

```php
Expand Down
Loading