fast-forward / http-message
Fast Forward PSR-7 HTTP Message utility classes
Requires
- php: ^8.3
- nyholm/psr7: ^1.8
- psr/http-message: ^2.0
Requires (Dev)
- fast-forward/dev-tools: dev-main
README
A modern PHP library for working with PSR-7 HTTP Messages, focusing on immutability, strict typing, and developer ergonomics. Part of the Fast Forward ecosystem.
✨ Features
- ✅ Fully PSR-7 compliant
- ✅ Strictly typed for PHP 8.3+
- ✅ Immutable by design (PSR-7 standard)
- ✅ Convenient JSON, HTML, Text, Empty, and Redirect responses
- ✅ Payload-aware interfaces for reusable patterns
- ✅ No external dependencies beyond PSR-7
📦 Installation
composer require fast-forward/http-message
Requirements: PHP 8.3+, Composer
🛠️ Usage
JSON Response Example
use FastForward\Http\Message\JsonResponse; $response = new JsonResponse(['success' => true]); echo $response->getStatusCode(); // 200 echo $response->getHeaderLine('Content-Type'); // application/json; charset=utf-8 echo (string) $response->getBody(); // {"success":true}
Payload Access and Immutability
$newResponse = $response->withPayload(['success' => false]); echo $response->getPayload()['success']; // true echo $newResponse->getPayload()['success']; // false
Text Response Example
use FastForward\Http\Message\TextResponse; $text = new TextResponse('Hello, world!');
Empty Response Example
use FastForward\Http\Message\EmptyResponse; $empty = new EmptyResponse();
Redirect Response Example
use FastForward\Http\Message\RedirectResponse; $redirect = new RedirectResponse('https://example.com', true); // Permanent redirect
🧰 API Summary
| Class/Method | Description | Docs |
|---|---|---|
| JsonResponse | JSON response with automatic headers | docs |
| TextResponse | Plain text response | docs |
| HtmlResponse | HTML response with correct Content-Type | docs |
| EmptyResponse | HTTP 204 No Content response | docs |
| RedirectResponse | HTTP redirect response (301/302) | docs |
| getPayload() | Returns the payload of the response | docs |
| withPayload($data) | Returns a new instance with a different payload | docs |
🔌 Integration
- Works out of the box with any PSR-7 compatible framework or library
- Designed to extend and complement
nyholm/psr7 - Can be used with fast-forward/container for dependency injection
📁 Directory Structure Example
src/
├── EmptyResponse.php
├── HtmlResponse.php
├── JsonResponse.php
├── TextResponse.php
├── RedirectResponse.php
├── ...
⚙️ Advanced/Customization
- Extend any response class to add custom logic
- Compose with other PSR-7 middlewares or response decorators
- All interfaces are public and designed for extension
🛠️ Versioning & Breaking Changes
- v1.4: PHP 8.3+ required, stricter typing, improved docs
- v1.0: Initial release
❓ FAQ
Q: What PHP version is required?
A: PHP 8.3 or higher.
Q: Is this library PSR-7 compliant?
A: Yes, all responses and streams are fully PSR-7 compliant and extend Nyholm's implementation.
Q: How do I create a JSON response?
A: Use JsonResponse and pass your payload to the constructor. The Content-Type header is set automatically.
Q: How do I work with payloads?
A: Use getPayload() to retrieve the payload and withPayload($payload) to create a new instance with a different payload.
Q: Can I use this with any framework?
A: Yes, as long as your framework supports PSR-7 messages.
Q: How do I handle authentication headers?
A: Use the Authorization header utility and credential classes for parsing and handling authentication schemes.
🛡 License
MIT © 2026 Felipe Sayão Lobato Abreu
🤝 Contributing
Contributions, issues, and feature requests are welcome!
Feel free to open a GitHub Issue or submit a Pull Request.