From 294a22a7421e232347d0ceb9d3950fa212a60cfb Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Mon, 29 Jun 2026 20:55:49 +0300 Subject: [PATCH] Fix #488: Document API content negotiation --- src/guide/rest/quick-start.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/guide/rest/quick-start.md b/src/guide/rest/quick-start.md index 73629571..469c4678 100644 --- a/src/guide/rest/quick-start.md +++ b/src/guide/rest/quick-start.md @@ -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