【问题标题】:How to add an extra information on api-platform result如何在 api-platform 结果上添加额外信息
【发布时间】:2019-04-30 12:59:10
【问题描述】:

我正在使用 symfony 和 api 平台。

我有一个资源:

/**
 * @ApiResource()
 */
class Credit
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="integer")
     */
    private $value; 
}

/api/credits 的结果是:

{
    "@context": "/api/contexts/Credit",
    "@id": "/api/credits",
    "@type": "hydra:Collection",
    "hydra:member": [
        {
            "@id": "/api/credits/1",
            "@type": "Credit",
            "id": 1,
            "value": 200,
            "createdAt": "2019-03"
        },
        {
            "@id": "/api/credits/2",
            "@type": "Credit",
            "id": 2,
            "value": 200,
            "createdAt": "2019-04"
        }
    ],
    "hydra:totalItems": 2
}

我想在结果中添加额外信息,例如 totalValues : 400(所有结果的“值”之和)

我该怎么做

预期结果:

  {
        "@context": "/api/contexts/Credit",
        "@id": "/api/credits",
        "@type": "hydra:Collection",
        "hydra:member": [
            {
                "@id": "/api/credits/1",
                "@type": "Credit",
                "id": 1,
                "value": 200,
                "createdAt": "2019-03"
            },
            {
                "@id": "/api/credits/2",
                "@type": "Credit",
                "id": 2,
                "value": 200,
                "createdAt": "2019-04"
            }
        ],
        "hydra:totalItems": 2,
        "totalValues": 400 
    }

【问题讨论】:

  • 这个答案可能对你有帮助:stackoverflow.com/a/52592464/1545904
  • 感谢您的回复,但这为我添加了每个元素的信息,但我想添加孔结果的信息,例如 totalItems
  • here 我们必须实现一个集合规范器

标签: symfony doctrine api-platform.com


【解决方案1】:

解决方案是像这样实现 NormalizerInterface 和 NormalizerAwareInterface :

ApiCollectionNormalizer:

namespace App\Serializer;

use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

final class ApiCollectionNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
    /**
     * @var NormalizerInterface|NormalizerAwareInterface
     */
    private $decorated;

    public function __construct(NormalizerInterface $decorated)
    {
        if (!$decorated instanceof NormalizerAwareInterface) {
            throw new \InvalidArgumentException(
                sprintf('The decorated normalizer must implement the %s.', NormalizerAwareInterface::class)
            );
        }
        $this->decorated = $decorated;
    }

    /**
     * @inheritdoc
     */
    public function normalize($object, $format = null, array $context = [])
    {
        $data = $this->decorated->normalize($object, $format, $context);
        if ('collection' === $context['operation_type'] && 'get' === $context['collection_operation_name']) {
            if ($data['@id'] === '/api/credits') {
                $totalValues = 0;
                foreach ($data['hydra:member'] as $credit) {
                    $totalValues += $credit['value'];
                }
                $data['totalValues'] = $totalValues;
            }

        }
        return $data;
    }

    /**
     * @inheritdoc
     */
    public function supportsNormalization($data, $format = null)
    {
        return $this->decorated->supportsNormalization($data, $format);
    }

    /**
     * @inheritdoc
     */
    public function setNormalizer(NormalizerInterface $normalizer)
    {
        $this->decorated->setNormalizer($normalizer);
    }

}

services.yaml:

  'App\Serializer\ApiCollectionNormalizer':
    decorates: 'api_platform.hydra.normalizer.collection'
    arguments: [ '@App\Serializer\ApiCollectionNormalizer.inner' ]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    • 2017-05-26
    • 1970-01-01
    • 2019-11-10
    • 2023-03-08
    相关资源
    最近更新 更多