echo-fusion / responsemanager
PSR-7 HTML, JSON, XML and redirect response classes for the EchoFusion framework.
Requires
- php: ~8.1.0 || ~8.2.0 || ~8.3.0
- guzzlehttp/psr7: ^2.5
- psr/http-message: ^2.0
Requires (Dev)
- echo-fusion/codestandard: ^1.0
- friendsofphp/php-cs-fixer: dev-master
- phpunit/phpunit: ^9.5
- vimeo/psalm: 5.x-dev
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-10-12 11:10:57 UTC
README
ResponseManager provides ready-made PSR-7 response classes for the EchoFusion framework: HTML, JSON, XML and redirect responses. They extend GuzzleHttp\Psr7\Response, so they work anywhere a Psr\Http\Message\ResponseInterface is expected.
Part of EchoFusion, a modular PHP framework built from small packages.
Installation
composer require echo-fusion/responsemanager
Requirements
- PHP 8.1, 8.2 or 8.3
Usage
All constructors take an optional status code and headers array. A Content-Type (or Location) header you pass yourself is never overwritten, whatever its letter case.
HTML
use EchoFusion\ResponseManager\HtmlResponse; return new HtmlResponse('<h1>Hello</h1>'); // 200, text/html; charset=utf-8 return new HtmlResponse($html, 404);
JSON
use EchoFusion\ResponseManager\JsonResponse; return new JsonResponse(['id' => 1], 201); // application/json
Data is encoded with safe defaults (JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_SLASHES); pass your own flags as the fourth argument. Data that cannot be encoded throws EchoFusion\ResponseManager\Exceptions\JsonEncodingException. getPayload() returns the original data (objects are cloned).
XML
use EchoFusion\ResponseManager\XmlResponse; return new XmlResponse('<user id="1"/>'); // application/xml; charset=utf-8
Redirect
use EchoFusion\ResponseManager\RedirectResponse; return new RedirectResponse('/login'); // 302 return new RedirectResponse('/new-url', 301);
The status must be 3xx, otherwise an InvalidArgumentException is thrown. The URL is HTML-escaped in the fallback body.
Testing
composer test
Credits
License
The MIT License (MIT). Please see LICENSE for more information.