Last active
August 4, 2017 10:11
-
-
Save darrena092/f5e89260558abadf92f5471a22f3f952 to your computer and use it in GitHub Desktop.
Generating public URLs for Sonata Media objects during serialization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace AppBundle\Handlers; | |
use Application\Sonata\MediaBundle\Entity\Media; | |
use JMS\Serializer\Context; | |
use JMS\Serializer\GraphNavigator; | |
use JMS\Serializer\Handler\SubscribingHandlerInterface; | |
use JMS\Serializer\JsonSerializationVisitor; | |
class MediaHandler implements SubscribingHandlerInterface { | |
private $container; | |
public function __construct($container) { | |
$this->container = $container; | |
} | |
public static function getSubscribingMethods() | |
{ | |
return [ | |
[ | |
'direction' => GraphNavigator::DIRECTION_SERIALIZATION, | |
'format' => 'json', | |
'type' => 'Application\Sonata\MediaBundle\Entity\Media', | |
'method' => 'serializeMedia' | |
] | |
]; | |
} | |
public function serializeMedia(JsonSerializationVisitor $visitor, Media $media, array $type, Context $context) { | |
$mediaHelper = $this->container->get('templating.helper.media'); | |
return $mediaHelper->getPublicUrl($media, 'default'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Helpers; | |
use Application\Sonata\MediaBundle\Entity\Media; | |
use Symfony\Component\Templating\Helper\Helper; | |
class MediaHelper extends Helper { | |
private $container; | |
public function __construct($container) { | |
$this->container = $container; | |
} | |
public function getPublicUrl(Media $media, $size) { | |
$provider = $this->container->get($media->getProviderName()); | |
return $provider->generatePublicUrl($media, $size); | |
} | |
public function getName() { | |
return "MediaHelper"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
templating.helper.media: | |
class: AppBundle\Helpers\MediaHelper | |
arguments: | |
- '@service_container' | |
tags: | |
- { name: templating.mediahelper, alias: mediahelper } | |
jms_media_handler: | |
class: AppBundle\Handlers\MediaHandler | |
arguments: | |
- '@service_container' | |
tags: | |
- { name: jms_serializer.subscribing_handler } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Controller; | |
use AppBundle\Entity\Stuff; | |
use JMS\Serializer\Handler\HandlerRegistry; | |
use JMS\Serializer\SerializerBuilder; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
use Symfony\Component\HttpFoundation\Response; | |
class StuffController extends Controller | |
{ | |
/** | |
* @Route("/api/stuff/list", name="list_stuff") | |
*/ | |
public function listStuffAction() | |
{ | |
$stuff = $this->getDoctrine()->getRepository(Stuff::class) | |
->findAll(); | |
// Use our custom handler. | |
$serializer = SerializerBuilder::create() | |
->configureHandlers(function(HandlerRegistry $registry) { | |
$registry->registerSubscribingHandler($this->get('jms_media_handler')); | |
}) | |
->addDefaultHandlers() | |
->build(); | |
$data = $serializer->serialize($stuff, 'json'); | |
$response = new Response($data); | |
$response->headers->set('Content-Type', 'application/json'); | |
return $response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment