kr0lik / laravel-param-converter
Symfony param converter with json serialization working in Laravel.
0.0.5
2024-01-18 09:13 UTC
Requires
- php: ^8.2
- doctrine/annotations: ^2.0
- laravel/framework: ^10.0
- phpdocumentor/reflection-docblock: ^5.3
- symfony/cache: ^7.0
- symfony/http-foundation: ^6.4
- symfony/property-access: ^7.0
- symfony/property-info: ^7.0
- symfony/serializer: ^7.0
- symfony/validator: ^7.0
Requires (Dev)
- icanhazstring/composer-unused: ^0.8.11
- kr0lik/php-cs-fixer: ^2.0
- kr0lik/phpstan-rules: ^2.0
README
Request ParamConverter with json serialization
Install
$ composer require kr0lik/laravel-param-converter
Setup is extremely simple, just add the service provider to your app.php config.
Kr0lik\ParamConverter\ParamConverterServiceProvider::class,
You can also publish the config so you can add your own param converters.
$ php artisan vendor:publish --provider="Kr0lik\ParamConverter\ParamConverterServiceProvider"
Usage
Add Config:
<?php declare(strict_types=1); use Kr0lik\ParamConverter\Converter\RequestDataConverter; use Kr0lik\ParamConverter\Converter\QueryParamConverter; return [ 'request' => [ 'autoConvert' => true, ], 'converters' => [ RequestDataConverter::NAME => RequestDataConverter::class, QueryParamConverter::NAME => QueryParamConverter::class, ], ];
Create dto:
<?php declare(strict_types=1); use Kr0lik\ParamConverter\Contract\RequestDtoInterface; use Symfony\Component\Validator\Constraints as Assert; final readonly class TextDto implements RequestDtoInterface { public function __construct( #[Assert\GreaterThanOrEqual(10)] public int $a, #[Assert\NotBlank()] public string $b, public DateTimeImmutable $c, ) { } }
Create action or controller:
<?php use Kr0lik\ParamConverter\Annotation\ParamConverter; class YourAction extends Controller { #[ParamConverter('token', converter: QueryParamConverter::NAME)] #[ParamConverter("requestDto", class=TextDto::class)] public function __invoke(string $token, TextDto $requestDto) { .... } }
or
Add annotation if in config set autoConvert = true
:
<?php use Kr0lik\ParamConverter\Annotation\ParamConverter; class YourAction extends Controller { public function __invoke(TextDto $requestDto) { .... } }