Standard-Compliant HTTP Library with Extended Features
A PSR and RFC compliant HTTP library that provides elegant request/response handling, content negotiation and problem details for PHP applications.
Why Derafu\Http?
🎯 Simple, but not limited, HTTP Handling
Most HTTP libraries either do too much or too little. Derafu\Http provides:
- Extended Request/Response: Smart content type negotiation and safe data access.
- Content Negotiation: Intelligent format detection and response transformation.
- Problem Details: RFC 7807 implementation for structured error handling.
- PSR Compliance: Built on PSR-7 and PSR-15 standards.
- Middleware Architecture: Flexible request/response processing pipeline.
✨ Key Features
- Smart Request Handling: Safe access to query, post, and JSON data.
- Intelligent Responses: Automatic content negotiation and format transformation.
- Structured Errors: Complete RFC 7807 Problem Details implementation.
- Modular Design: Core HTTP functionality with middleware support.
- Type Safety: Use of enums for HTTP status codes and content types.
- Middleware Pipeline: PSR-15 compliant middleware chain for request processing.
Installation
composer require derafu/http
Basic Usage
public/index.php
use Derafu\Http\Kernel;
use Derafu\Kernel\Environment;
require_once dirname(__DIR__) . '/app/bootstrap.php';
return fn (array $context): Kernel => new Kernel(new Environment(
$context['APP_ENV'],
(bool) $context['APP_DEBUG'],
$context
));
A first route
A route handler can return plain data. The response is built from it using the content type the client asks for:
# config/routes.yaml
hello:
path: /api/hello
handler: App\Controller\HelloController::index
class HelloController
{
public function index(): array
{
// Sent as {"status":"ok"} with Content-Type: application/json.
return ['status' => 'ok'];
}
}
Where to Go Next
| Page | What it covers |
|---|---|
| HTTP Flow | How a request travels from the client to the response, and what happens when something fails |
| Middleware | The middleware stack, the core and optional middlewares, and how to write your own |
| Requests and Responses | The Request and Response API, content negotiation and the enums |
| Error Handling | Problem Details (RFC 7807), HTTP exceptions and error pages |
| Testing | How to test the real middleware stack |
| Design Principles | The ideas behind the library |
Integration with Other Packages
Derafu\Http is designed to work with other Derafu packages:
Required
- derafu/kernel: The core of the application, with dependency injection.
- derafu/renderer: For template rendering, with dependency on derafu/twig.
- derafu/routing: For URL routing, with autodiscovery of templates.
- derafu/translation: For I18n, with a simple translator that supports ICU.
Optional
- derafu/markdown: For rendering templates in markdown format.
- symfony/rate-limiter: For the
ThrottleMiddleware.
Suggested
- derafu/data-processor: For data processing: format, cast, sanitize and validate.
On this page
Last updated on 10/10/2026
by Anonymous