Download the PHP package methorz/http-dto without Composer
On this page you can find all versions of the php package methorz/http-dto. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download methorz/http-dto
More information about methorz/http-dto
Files in methorz/http-dto
Package http-dto
Short Description Automatic HTTP ↔ DTO conversion via PSR-15 middleware
License MIT
Informations about the package http-dto
methorz/http-dto
Automatic HTTP ↔ DTO conversion with validation for PSR-15 applications
What is this?
This package provides automatic Data Transfer Object (DTO) handling for PSR-15 middleware applications (Mezzio, Laminas) using the DtoHandlerWrapper pattern. It eliminates boilerplate code by:
- 🎯 Automatically extracting data from HTTP requests (JSON body, query params, route attributes)
- 🔄 Automatically mapping request data to Request DTOs
- ✅ Automatically validating Request DTOs using Symfony Validator
- 🚀 Automatically injecting validated DTOs as handler parameters
- 📦 Automatically serializing Response DTOs to JSON responses
Key Innovation: The DtoHandlerWrapper pattern wraps your DtoHandlerInterface implementations, handling all DTO concerns without requiring global middleware in your pipeline.
Installation
Quick Example
Before (Manual Boilerplate)
After (Automatic! ✨)
Features
1. Automatic Request → DTO Mapping
Define Request DTOs with Symfony Validator attributes:
2. Automatic DTO → Response Serialization
Define Response DTOs with JsonSerializableDto:
3. Handler with Direct DTO Parameters
Implement DtoHandlerInterface and use __invoke():
Setup
1. Register DtoHandlerWrapperFactory in ConfigProvider
2. Wrap Your Handlers in Routes
Use DtoHandlerWrapperFactory to wrap your DtoHandlerInterface implementations:
Complete Flow
Key Benefits of DtoHandlerWrapper Pattern:
- ✅ Single pattern - One component handles request DTO mapping AND response serialization
- ✅ Handler-specific - Only processes requests to DtoHandlerInterface implementations
- ✅ No middleware overhead - Doesn't process every request in the pipeline
- ✅ Cleaner architecture - Clear separation: middleware for cross-cutting, wrapper for handler-specific
- ✅ Easy to use - Just wrap your handler in routes configuration
Error Handling
The middleware automatically handles validation errors:
Benefits
For Handlers
✅ Return DTOs directly (not ResponseInterface)
✅ No ApiResponse wrapper calls
✅ No manual ->toArray() calls
✅ Perfect type safety
✅ Ultra clean (often one line!)
For Response DTOs
✅ Control their own HTTP status code
✅ Self-serializing (jsonSerialize())
✅ Single Responsibility Principle
For Testing
✅ Test handler returns actual DTO
✅ No mocking ApiResponse
✅ Test serialization separately
✅ More maintainable
For Architecture
✅ Perfect symmetry: Request DTOs IN, Response DTOs OUT ✅ Consistent pattern across all handlers ✅ Type-safe end-to-end
Requirements
- PHP 8.2+
- PSR-7 (HTTP Message Interface)
- PSR-15 (HTTP Server Middleware)
- Symfony Validator
- Mezzio or any PSR-15 compatible framework
Related Packages
This package is part of the MethorZ HTTP middleware ecosystem:
| Package | Description |
|---|---|
| methorz/http-dto | Automatic HTTP ↔ DTO conversion (this package) |
| methorz/http-problem-details | RFC 7807 error handling middleware |
| methorz/http-cache-middleware | HTTP caching with ETag support |
| methorz/http-request-logger | Structured logging with request tracking |
| methorz/openapi-generator | Automatic OpenAPI spec generation from DTOs |
These packages work together seamlessly in PSR-15 applications.
License
MIT License. See LICENSE file for details.
Author
Thorsten Merz
Made with ❤️ for clean, type-safe APIs
All versions of http-dto with dependencies
psr/container Version ^2.0
psr/http-factory Version ^1.0
psr/http-message Version ^1.0 || ^2.0
psr/http-server-handler Version ^1.0
psr/http-server-middleware Version ^1.0
ramsey/uuid Version ^4.9