boshurik/api-bundle

构建API的有用服务集合

0.3.1 2021-04-16 14:00 UTC

This package is auto-updated.

Last update: 2024-09-16 22:01:24 UTC


README

Build Status

构建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);
}