sabre/http
sabre/http is a lightweight toolkit for working with HTTP in PHP. It wraps superglobals and output functions into extendable, mockable Request and Response objects, making it easier to read request data and send headers/body consistently across your app.
Begin by installing via Composer: composer require sabre/http. The core entry point is Sabre\HTTP\Sapi, used to build Request and Response objects from PHP’s native superglobals and output mechanisms. Instantiate the request once at application bootstrap:
$request = Sabre\HTTP\Sapi::getRequest();
Then inject RequestInterface everywhere instead of relying on $_GET, $_POST, or $_SERVER. For responses, construct a Response, configure it, and dispatch it once at the end:
$response = new Sabre\HTTP\Response();
$response->setBody('OK');
Sabre\HTTP\Sapi::sendResponse($response);
Start by reading examples/ in the repo — especially reverseproxy.php — to see real-world usage.
RequestInterface and ResponseInterface through layers (controllers, services, middleware). Avoid calling Sapi::getRequest() multiple times.AuthenticatedRequest, LoggedResponse) to add domain-specific methods like isLoggedIn() or logTimestamp().Mockery or custom RequestInterface implementations into tests — no reliance on global state.HTTP\Client for lightweight API calls. Combine with on('error') hooks to implement retries or auth injection. Prefer async mode for batch external calls (sendAsync() + wait()).Client, and echoing the response back with Sapi::sendResponse().getBodyAsStream() returns a non-seekable resource — read only once. Convert to string only once via getBodyAsString() (subsequent calls may fail or return empty).getHeader('Content-Type'), not getHeader('content-type'). But setHeader() preserves original casing.Request — ensure your APIs accept only RequestInterface.Sapi::getRequest() only reads from superglobals. In CLI or non-web contexts, construct Request manually or use Sapi::getRequestFromServer() for unit testing.$client->wait() is blocking — avoid mixing sync/async unless you buffer results.sabre/http is mature but low activity; consider symfony/http-foundation for larger frameworks unless you need its simpler, decorator-friendly design.How can I help you explore Laravel packages today?