我正在尝试向特定资源添加一些额外的数据,因此我遵循了有关装饰序列化程序和添加额外数据的文档,但是当我测试资源时出现此错误:
调用 (...)/vendor/api-platform/core/src/Serializer/AbstractItemNormalizer.php 中的 null 成员函数 normalize(),第 426 行。
我已经为这个错误苦苦挣扎了将近两天,我不知道我是否遗漏了一些细节,或者这不是向资源添加额外数据的正确方法。这是我的资源定义:
AppBundle\Entity\MediaGenerator\Teaser:
attributes:
access_control: "is_granted('ROLE_ADMIN')"
normalization_context:
groups: ['teaser','teaser-read']
denormalization_context:
groups: ['teaser','teaser-write']
order:
position: 'ASC'
这是我的自定义规范器:
class ApiNormalizer implements NormalizerInterface
{
private $normalizer;
public function __construct(NormalizerInterface $normalizer)
{
$this->normalizer = $normalizer;
}
public function supportsNormalization($data, $format = null)
{
return $data instanceof Teaser;
}
public function normalize($object, $format = null, array $context = [])
{
$data = $this->normalizer->normalize($object, $format, $context);
if (is_array($data)) {
$data['view'] = "SOME TRANSFORMATION ON TEASER VIEW";
}
return $data;
}
public function supportsDenormalization($data, $type, $format = null)
{
return $this->normalizer->supportsNormalization($data, $type, $format);
}
public function denormalize($data, $class, $format = null, array $context = [])
{
return $this->normalizer->denormalise($data, $class, $format, $context);
}
}
这是我的 services.yml 声明:
AppBundle\Serializer\ApiNormalizer:
decorates: 'api_platform.jsonld.normalizer.item'
arguments: [ '@AppBundle\Serializer\ApiNormalizer.inner' ]
autoconfigure: false
其他一切都作为一种魅力,config.yml 中的序列化程序启用为{ enable_annotations: true }
.
有什么硬仗吗?
在此先感谢 ApiPlatform 的出色工作!