shooktea/request-deserializer-bundle
Composer 安装命令:
composer require shooktea/request-deserializer-bundle
包简介
Small bundle for deserializing and validating requests from JSON to objects
README 文档
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/serializerandsymfony/validatorcomponents 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".
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-01-24