HTML Response
HtmlResponse
is the simplest way to return HTML while keeping PSR-7 compatibility.
What It Does
- sets the response status to HTTP 200;
- sets
Content-Typetotext/htmlwith a charset; - writes the provided HTML string into the body.
Basic Example
use FastForward\Http\Message\HtmlResponse;
$response = new HtmlResponse('<h1>Hello</h1>');
echo $response->getHeaderLine('Content-Type'); // text/html; charset=utf-8
Custom Charset
$response = new HtmlResponse(
html: '<p>Olá</p>',
charset: 'iso-8859-1',
);
Additional Headers
$response = new HtmlResponse(
'<h1>Dashboard</h1>',
headers: ['Cache-Control' => 'no-store'],
);
When to Choose HTML Over JSON
Use HtmlResponse
when the client is expected to render markup directly, such as:
- a small PSR-15 web endpoint;
- a maintenance page;
- a fallback view for browser requests.
If you need to choose dynamically between HTML and JSON, see Header Utilities
for Accept
negotiation.