私はこの答えに従おうとしました: JMSシリアライザーバンドルを使用して追加のフィールドを追加します
でも変わらない..
送信する前に、シリアル化されたエンティティ (json) に追加のフィールドを追加したいと考えています。見逃したものはありますか?
これが私のリスナーです:
<?php
namespace My\MyBundle\Listener;
use JMS\DiExtraBundle\Annotation\Service;
use JMS\DiExtraBundle\Annotation\Tag;
use JMS\DiExtraBundle\Annotation\Inject;
use JMS\DiExtraBundle\Annotation\InjectParams;
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
use My\MyBundle\Entity\Dossier;
use JMS\Serializer\Handler\SubscribingHandlerInterface;
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
use JMS\Serializer\EventDispatcher\PreSerializeEvent;
use JMS\Serializer\EventDispatcher\ObjectEvent;
use JMS\Serializer\GraphNavigator;
use JMS\Serializer\JsonSerializationVisitor;
/**
* Add data after serialization
*
* @Service("my.listener.serializationlistener")
* @Tag("jms_serializer.event_subscriber")
*/
class SerializationListener implements EventSubscriberInterface
{
/**
* @inheritdoc
*/
static public function getSubscribedEvents()
{
return array(
array('event' => 'serializer.post_serialize', 'class' => 'My\MyBundle\Entity\Dossier', 'method' => 'onPostSerialize'),
);
}
public function onPostSerialize(ObjectEvent $event)
{
$event->getVisitor()->addData('someKey','someValue');
}
}
そして私のコントローラーでの呼び出し:
$serializer = $this->container->get('jms_serializer');
$res = $serializer->serialize($dossier, 'json');
次のサービス宣言も追加します。
services:
my.mybundle.listener:
class: My\MyBundle\Listener\SerializationListener
私は別のサービスを宣言しており、リスナーサービスでそれを行うときではなく、その宣言名を変更するとsymfonyが与えてエラーになります。
前もって感謝します