boshurik / api-bundle
构建API的有用服务集合
0.3.1
2021-04-16 14:00 UTC
Requires
- php: ^7.4|^8.0
- symfony/framework-bundle: ^5.0
- symfony/serializer: ^5.0
- symfony/validator: ^5.0
Requires (Dev)
- doctrine/persistence: ^2.0
- friendsofphp/php-cs-fixer: ^2.16
- psalm/plugin-symfony: ^2.0
- symfony/phpunit-bridge: ^5.0
- vimeo/psalm: ^4.0
Suggests
- doctrine/mongodb-odm: To use abstract object normalizer with documents
- doctrine/orm: To use abstract object normalizer with entities
README
构建API的有用服务集合
安装
Composer
$ composer require boshurik/api-bundle
如果您正在使用 symfony/flex,那么这就是您需要做的
注册扩展包
<?php // config/bundles.php return [ //... \BoShurik\ApiBundle\BoShurikApiBundle => ['all' => true], ];
使用
ArgumentResolver
class SomeController { public function someAction(SomeModel $model) { // $model - validated object } }
Serializer
/** * @var SomeEntity $entity */ $data = $this->serializer->normalize($entity); // $entity->getId() value $entity = $this->serializer->denormalize('some-id', SomeEntity::class); // SomeEntity instant
ValidationException
$violations = $this->validator->validate($model); if ($violations->count() > 0) { throw new ValidationException($violations); }