Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Request Dto Resolver Bundle Laravel Package

crtl/request-dto-resolver-bundle

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • 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.

    • Pros:
      • Reduces boilerplate in Laravel controllers by automating DTO hydration/validation.
      • Supports nested DTOs, strict typing, and custom transformers—features lacking in Laravel’s native FormRequest.
      • Integrates with Symfony’s ValidatorInterface, which Laravel can leverage via symfony/validator.
    • Cons:
      • Laravel’s service container and request lifecycle differ from Symfony, requiring adaptation (e.g., custom resolvers, middleware).
      • No native Laravel-specific optimizations (e.g., Illuminate\Http\Request integration).
  • Key Features Leveraged:

    1. Automatic DTO Resolution: Replace Laravel’s manual FormRequest binding with type-hinted DTOs in controller methods.
    2. Strict Typing: Enforce PHP 8+ type safety (e.g., string, int) with runtime checks.
    3. Nested DTOs: Support complex payloads (e.g., BodyParamNestedDto).
    4. Validation Integration: Use Laravel’s Validator or Symfony’s ValidatorInterface for constraints.
    5. Query/Route/File/Headers: Bind all request sources to DTO properties.

Integration Feasibility

  • Symfony vs. Laravel Compatibility:

    • Core Logic: The bundle’s hydration/validation pipeline can be ported to Laravel via:
      • A custom Laravel service provider to register DTO resolvers.
      • Middleware to intercept requests and resolve DTOs before controller execution.
      • Laravel’s App\Providers\RouteServiceProvider to bind DTOs to routes.
    • Challenges:
      • Laravel’s request object (Illuminate\Http\Request) differs from Symfony’s (Symfony\Component\HttpFoundation\Request). A wrapper class may be needed.
      • Event system: Laravel uses Illuminate\Events; Symfony uses Symfony\Component\EventDispatcher. Custom event listeners would bridge this.
      • Validation: Laravel’s Validator and Symfony’s ValidatorInterface are similar but not identical. A compatibility layer (e.g., symfony/validator package) would be required.
  • Example Integration Path:

    1. Install dependencies:
      composer require symfony/validator symfony/http-foundation
      
    2. Create a DTO resolver service (Laravel’s 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())
              );
          });
      }
      
    3. Use in controllers:
      use App\DTO\ExampleDto;
      
      public function store(ExampleDto $dto) {
          // $dto is auto-resolved and validated
      }
      

Technical Risk

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.

Key Questions for TPM

  1. Adoption Scope:

    • Should this replace all FormRequest usage, or supplement it for complex DTOs?
    • Will the team accept Symfony dependencies (e.g., HttpFoundation)?
  2. Validation Strategy:

    • Should we use Symfony’s ValidatorInterface (via symfony/validator) or Laravel’s native Validator?
    • How will custom validation rules (e.g., Laravel’s Rule objects) integrate?
  3. Error Handling:

    • Should we keep the bundle’s RequestValidationException or adapt to Laravel’s ValidationException?
    • How will API error responses (e.g., JSON formatting) be standardized?
  4. Performance:

    • Will reflection-based hydration impact high-traffic endpoints?
    • Should we cache DTO metadata (e.g., property mappings) for repeated requests?
  5. Team Buy-In:

    • Is the team comfortable with attribute-based DTOs (#[RequestDto]) vs. Laravel’s FormRequest?
    • Will this reduce or increase boilerplate in controllers?
  6. Future-Proofing:

    • Should we fork the bundle to remove Symfony dependencies?
    • How will future Laravel versions (e.g., Symfony 7+ compatibility) affect this?

Integration Approach

Stack Fit

  • Laravel Compatibility:

    • Pros:
      • Laravel’s dependency injection and service container can host the bundle’s resolver.
      • PHP 8+ features (attributes, union types) align with the bundle’s design.
      • Validation can leverage Laravel’s Validator or Symfony’s ValidatorInterface.
    • Cons:
      • Request object differences: Laravel’s Illuminate\Http\Request vs. Symfony’s HttpFoundation\Request.
      • Event system: Laravel’s Events vs. Symfony’s EventDispatcher.
      • Routing: Laravel’s 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

Migration Path

Phase 1: Proof of Concept (2-4 weeks)

  1. Isolate a Single Controller:
    • Replace a FormRequest-based endpoint with a DTO-bound controller.
    • Example:
      // Before (FormRequest)
      public function store(CreateUserRequest $request) { ... }
      
      // After (DTO)
      public function store(CreateUserDto $dto) { ... }
      
  2. Implement a Minimal Resolver:
    • Create a custom service provider to register the bundle’s resolver.
    • Use a wrapper class to adapt Laravel’s Request to Symfony’s interface.
  3. Test Validation:
    • Verify that constraints (#[Assert\NotBlank]) and type hints work.
    • Check error responses (e.g., 400 Bad Request with validation details).

Phase 2: Core Integration (4-6 weeks)

  1. Standardize DTOs:
    • Refactor existing FormRequest classes into DTOs with #[RequestDto].
    • Example:
      #[RequestDto]
      class CreateUserDto {
          #[BodyParam, Assert\NotBlank]
          public string $name;
      
          #[QueryParam(transformType: "int")]
          public int $age;
      }
      
  2. Middleware Integration:
    • Add middleware to resolve DTOs before controller execution.
    • Example:
      public function handle(Request $request, Closure $next) {
          $resolver = app(RequestDtoResolver::class);
          $dto = $resolver->resolve($request, CreateUserDto::class);
          $request->attributes->set('dto', $dto
      
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
iio/libmergepdf
redaxo/project
zatona-eg/zatona-eg-api
patrickbussmann/oauth2-apple
3brs/enterprise-security-bundle
ardenexal/fhir-models
ardenexal/fhir-validation
dpfx/laravel-livewire-wizards
dmstr/symfony-system-resources-bundle
dmstr/symfony-job-queue-bundle
dmstr/openapi-json-schema-bundle
dmstr/keycloak-security-bundle
dmstr/doctrine-audit-log-bundle
dmstr/api-platform-utils-bundle
dmstr/api-configuration-bundle
chrisdev/ux-components
crudly/encrypted
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony