pipe / response
Blazingly fast php http response util
v0.2.0
2026-12-02 13:46 UTC
Requires
- php: >=8.2
Requires (Dev)
- pestphp/pest: ^3.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-12-02 13:47:04 UTC
README
Simple, pure PHP HTTP response builder
Installation
composer require pipe/response
Requires PHP 8.2 or newer. No runtime dependencies.
Usage
A response is built first and sent once. Builder methods only change the response; nothing is output until send().
use Pipe\Response; $response = new Response(); $response->html('<h1>Hello</h1>'); // 200, text/html; charset=UTF-8 $response->json(['success' => true], 201); // application/json $response->redirect('/login'); // 302 + Location, empty body $response->noContent(); // 204, empty body $response->status(404)->header('X-Trace', 'abc'); $response->send(); // status, headers, body. Exactly once. $response->send(withBody: false); // for HEAD requests
API
| Method | Description |
|---|---|
html(string $content, int $status = 200): self |
HTML body, text/html; charset=UTF-8. |
text(string $content, int $status = 200): self |
Plain-text body, text/plain; charset=UTF-8. |
json(mixed $data, int $status = 200, int $flags = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE): self |
JSON body, application/json. Throws JsonException when $data can't be encoded, so a broken payload never becomes an empty 200. |
redirect(string $url, int $status = 302): self |
Sets the Location header and an empty body. The status must be 3xx. |
noContent(int $status = 204): self |
Empty body, and removes Content-Type. |
status(int $code): self |
Status code, 100–599. |
content(string $content): self |
Body. |
header(string $name, string $value, bool $replace = true): self |
Sets a header. Names are case-insensitive. Pass $replace = false to add another value, for example several Set-Cookie headers. Invalid names and values containing CR, LF or NUL throw InvalidArgumentException, which prevents header injection. |
removeHeader(string $name): self |
Removes a header. |
getStatus(), getContent(), getHeaders(), getHeader(string $name) |
Read the response back. |
send(bool $withBody = true): self |
Outputs the status, headers and body. Throws LogicException if the response was already sent or if output has already started. No body is sent for 1xx, 204 and 304 responses. |
isSent(): bool |
Whether send() has run. |
Response::getInstance() / response() |
A shared instance. |
Testing
composer install
composer test
Philosophy
- Small is Beautiful.
- Do one thing,
- Do one thing well.
- Get the PoC out ASAP.
- Consider your code dead two weeks after inception.
- n'joy!
sounds familiar? It should!