crtl/request-dto-resolver-bundle
Alignment with Laravel/PHP Ecosystem: The bundle is designed for Symfony, not Laravel, but its core functionality (DTO resolution, validation, and request binding) is highly transferable to Laravel via custom service providers or middleware. Laravel’s request validation (e.g., Form Requests) and dependency injection already handle similar use cases, but this bundle offers explicit DTO binding with stricter typing and finer-grained control.
FormRequest.ValidatorInterface, which Laravel can leverage via symfony/validator.Illuminate\Http\Request integration).Key Features Leveraged:
FormRequest binding with type-hinted DTOs in controller methods.string, int) with runtime checks.BodyParam → NestedDto).Validator or Symfony’s ValidatorInterface for constraints.Symfony vs. Laravel Compatibility:
App\Providers\RouteServiceProvider to bind DTOs to routes.Illuminate\Http\Request) differs from Symfony’s (Symfony\Component\HttpFoundation\Request). A wrapper class may be needed.Illuminate\Events; Symfony uses Symfony\Component\EventDispatcher. Custom event listeners would bridge this.Validator and Symfony’s ValidatorInterface are similar but not identical. A compatibility layer (e.g., symfony/validator package) would be required.Example Integration Path:
composer require symfony/validator symfony/http-foundation
AppServiceProvider):
use Crtl\RequestDtoResolverBundle\Resolver\RequestDtoResolver;
use Symfony\Component\Validator\Validator\ValidatorInterface;
public function register()
{
$this->app->singleton(RequestDtoResolver::class, function ($app) {
return new RequestDtoResolver(
$app->make(ValidatorInterface::class),
// Custom request wrapper for Laravel
new LaravelRequestWrapper(request())
);
});
}
use App\DTO\ExampleDto;
public function store(ExampleDto $dto) {
// $dto is auto-resolved and validated
}
| Risk Area | Description | Mitigation Strategy |
|---|---|---|
| Symfony Dependency | Bundle relies on Symfony components (e.g., HttpFoundation). |
Use symfony/http-foundation as a Laravel dependency; wrap Laravel’s Request. |
| Validation Mismatch | Laravel’s Validator vs. Symfony’s ValidatorInterface differences. |
Standardize on symfony/validator or create a compatibility adapter. |
| Performance Overhead | Reflection-based hydration may slow request processing. | Benchmark and optimize with Laravel’s Cache facade for DTO metadata. |
| Breaking Changes | Bundle’s 3.0.0+ refactored hydration/validation flow. | Test thoroughly; consider forking if critical changes are needed. |
| Laravel-Specific Gaps | No native support for Laravel’s FormRequest or ApiResource. |
Hybrid approach: Use bundle for DTOs, FormRequest for legacy validation. |
Adoption Scope:
FormRequest usage, or supplement it for complex DTOs?HttpFoundation)?Validation Strategy:
ValidatorInterface (via symfony/validator) or Laravel’s native Validator?Rule objects) integrate?Error Handling:
RequestValidationException or adapt to Laravel’s ValidationException?Performance:
Team Buy-In:
#[RequestDto]) vs. Laravel’s FormRequest?Future-Proofing:
Laravel Compatibility:
Validator or Symfony’s ValidatorInterface.Illuminate\Http\Request vs. Symfony’s HttpFoundation\Request.Events vs. Symfony’s EventDispatcher.RouteServiceProvider vs. Symfony’s Routing component.Recommended Stack Additions:
| Component | Purpose | Laravel Equivalent/Package |
|---|---|---|
symfony/validator |
Standardize validation logic. | Illuminate/Validation (fallback) |
symfony/http-foundation |
Wrap Laravel’s Request for bundle compatibility. |
Custom LaravelRequestWrapper class |
symfony/event-dispatcher |
Bridge Symfony events to Laravel’s Events. |
Illuminate/Events (adapter layer) |
symfony/dependency-injection |
Host bundle services in Laravel’s container. | Native Laravel DI |
FormRequest-based endpoint with a DTO-bound controller.// Before (FormRequest)
public function store(CreateUserRequest $request) { ... }
// After (DTO)
public function store(CreateUserDto $dto) { ... }
Request to Symfony’s interface.#[Assert\NotBlank]) and type hints work.400 Bad Request with validation details).FormRequest classes into DTOs with #[RequestDto].#[RequestDto]
class CreateUserDto {
#[BodyParam, Assert\NotBlank]
public string $name;
#[QueryParam(transformType: "int")]
public int $age;
}
public function handle(Request $request, Closure $next) {
$resolver = app(RequestDtoResolver::class);
$dto = $resolver->resolve($request, CreateUserDto::class);
$request->attributes->set('dto', $dto
How can I help you explore Laravel packages today?