Derafu: HTTP - 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
Basic Usage
public/index.php
Middleware Configuration
The library uses PSR-15 middlewares for request processing. Configure your middleware stack in the services file, for example services.yaml
:
Core Middlewares
The library includes four essential middlewares that must be configured in order:
- RequestFactoryMiddleware: Converts PSR-7 requests to Derafu requests.
- RouterMiddleware: Handles URL routing and route matching.
- DispatcherMiddleware: Executes route handlers (controllers, closures or templates).
- ResponseNormalizerMiddleware: Ensures PSR-7 compliant responses.
Custom Middlewares
Create custom middlewares by implementing PSR-15’s MiddlewareInterface
:
Working with Requests
Creating Responses
Error Handling with Problem Details
Any exception will be treated as “Problem Detail” (RFC 7807). To have control over all fields, exceptions must implement HttpExceptionInterface
.
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 renderig templates in markdown format.
Suggested
- derafu/data-processor: For data processing: format, cast, sanitize and validate.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
License
This package is open-sourced software licensed under the MIT license.