Search by

pipe / response

unamatasanatarai

Blazingly fast php http response util

Package info

github.com/pipephp/response

pkg:composer/pipe/response

Statistics

Installs: 43

Dependents: 1

Suggesters: 1

Stars: 0

Open Issues: 0

v0.2.0 2026-12-02 13:46 UTC

This package is auto-updated.

Last update: 2026-12-02 13:47:04 UTC


README

Simple, pure PHP HTTP response builder

Latest Stable Version Total Downloads License

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

  1. Small is Beautiful.
    • Do one thing,
    • Do one thing well.
  2. Get the PoC out ASAP.
    • Consider your code dead two weeks after inception.
  3. n'joy!

sounds familiar? It should!