artyuum/request-dto-mapper-bundle
composer require vendor/package-name
config/app.php under the extra.bundles key.use Vendor\Package\Attribute\Dto;
class UserController extends Controller
{
#[Dto]
public function store(Request $request)
{
// DTO validation and binding handled automatically
}
}
use Symfony\Component\Validator\Constraints as Assert;
class CreateUserDto
{
#[Assert\NotBlank]
public string $name;
#[Assert\Email]
public string $email;
}
Convert a controller action to use DTOs for validation and type safety:
#[Dto(CreateUserDto::class)]
public function store(Request $request, CreateUserDto $dto)
{
// $dto is automatically validated and populated
$user = User::create($dto->toArray());
return response()->json($user);
}
#[Dto] attribute over manual configuration for simplicity.PostDtoValidationEvent to customize behavior post-validation:
event(new PostDtoValidationEvent($dto, $request));
#[Dto(CreateUserDto::class, source: 'data.user')]
public function store(Request $request)
$errors = $request->get('dto_validation_errors', []);
config/packages/vendor_package.php:
'default_options' => [
'throw_exceptions' => false,
'validator' => 'symfony', // or 'laravel'
],
#[Dto(CreateUserDto::class, options: ['throw_exceptions' => true])]
kernel.controller_arguments event for testing:
$this->app->dispatch(new ControllerArgumentsEvent($request, $controller, $method));
SourceExtractionException when data extraction fails:
try {
$dto = new CreateUserDto($request->get('data'));
} catch (SourceExtractionException $e) {
abort(400, 'Invalid request data structure');
}
dd($request->get('dto_validation_errors')) to inspect stored violations.options: ['validate' => false].Vendor\Package\Contract\ValidatorInterface for non-Symfony validation.PostDtoValidationEvent for post-processing:
public static function getSubscribedEvents()
{
return [
PostDtoValidationEvent::class => 'onDtoValidated',
];
}
symfony/validator but remains compatible with Laravel’s validator. Ensure constraints are compatible with your chosen validator.composer.json:
"autoload": {
"psr-4": { "App\\": "app/" },
"files": ["vendor/vendor/package/src/Attribute/Dto.php"]
}
How can I help you explore Laravel packages today?