Search by

echo-fusion / responsemanager

amir-shadanfar

PSR-7 HTML, JSON, XML and redirect response classes for the EchoFusion framework.

Package info

github.com/echo-fusion/responsemanager

pkg:composer/echo-fusion/responsemanager

Statistics

Installs: 265

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-10-12 09:41 UTC

This package is auto-updated.

Last update: 2026-10-12 11:10:57 UTC


README

CI Packagist PHP License

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.