Skip to content

Instantly share code, notes, and snippets.

@Keeo
Last active March 26, 2017 17:20
Show Gist options
  • Save Keeo/edcc96366921edd3b0b8 to your computer and use it in GitHub Desktop.
Save Keeo/edcc96366921edd3b0b8 to your computer and use it in GitHub Desktop.
JMS serializer bundle visitor example - how to add another field into serialized entity
<?php
namespace Acme\DemoBundle\Serializer;
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
use JMS\Serializer\EventDispatcher\ObjectEvent;
class ExampleVisitor implements EventSubscriberInterface
{
static public function getSubscribedEvents()
{
return array(
array('event' => 'serializer.post_serialize', 'class' => 'Acme\DemoBundle\Entity\Example', 'method' => 'onPostSerialize'),
);
}
public function onPostSerialize(ObjectEvent $event)
{
$event->getVisitor()->addData('someKey','someValue');
}
}
treewer.serializer.company:
class: Acme\DemoBundle\Serializer\ExampleVisitor
tags:
- { name: jms_serializer.event_subscriber }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment