shooktea / request-deserializer-bundle
Small bundle for deserializing and validating requests from JSON to objects
Installs: 3
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: 8.*
- symfony/framework-bundle: 6.*
- symfony/serializer: 6.*
- symfony/validator: 6.*
README
Tested on PHP 8.1 with Symfony 6.0.
Installation
Install with Composer:
composer require shooktea/request-deserializer-bundle
And add bundle to bundles.php
(if was not added automatically by Symfony Flex):
<?php return [ Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true], // ... ShookTea\RequestDeserializerBundle\ShookTeaRequestDeserializerBundle::class => ['all' => true], ];
How to use it?
- Create a class implementing
ShookTea\RequestDeserializerBundle\Query\QueryInterface
(for GET parameters) orShookTea\RequestDeserializerBundle\Request\RequestInterface
(for POST parameters) - add annotations from
symfony/serializer
andsymfony/validator
components if necessary - inject your class to controller method
And that's it!
Example query class: (for GET parameters)
<?php use ShookTea\RequestDeserializerBundle\Query\QueryInterface; use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Serializer\Annotation\SerializedName; class ExampleQuery implements QueryInterface { #[SerializedName('id')] #[Assert\Uuid] public string $uuid; }
Usage in controller:
class ExampleController extends AbstractController { #[Route('/some/route')] public function someAction(ExampleQuery $query) { // do something with query } }
After requesting /some/route?id=217c26de-132e-49f7-a17d-fd80a70e3fe3
, in someAction()
method of controller,
object $query
will have a $uuid
with value "217c26de-132e-49f7-a17d-fd80a70e3fe3".