Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create a good hypermedia format using JMSSerializerBundle?
    text
    copied!<p>Lets say I want to create an XML-response that will looks something like the following:</p> <pre><code>&lt;?xml version="1.0" encoding="utf‐8"?&gt; &lt;product xmlns="urn:com.acme.prods" xmlns:atom="http://www.w3.org/2005/xlink"&gt; &lt;id&gt;1234&lt;/id&gt; &lt;name&gt;Red Stapler&lt;/name&gt; &lt;price currency="EUR"&gt;3.14&lt;/price&gt; &lt;atom:link rel="payment" type="application/com.acme.shop+xml" href="http://acme.com/products/1234/payment" /&gt; &lt;/product&gt; </code></pre> <p>Given an domain model that looks something like the following:</p> <pre><code>&lt;?php // Product.php namespace Acme\Bundle\ProductBundle\Entity; use Acme\Bundle\ProductBundle\Money\Money; class Product { /** * @var integer */ private $id; /** * @var string */ private $name; /** * @var Money */ private $price; [..] } </code></pre> <p>And a money-class along the lines of:</p> <pre><code>&lt;?php // Money.php namespace Acme\Bundle\ProductBundle\Money; class Money { /** * @var string */ private $currency; /** * */ private $amount; } </code></pre> <p>Now, to my questions. It would be pretty simple to create a response that looks like the following</p> <pre><code>&lt;?xml version="1.0" encoding="utf‐8"?&gt; &lt;product&gt; &lt;id&gt;1234&lt;/id&gt; &lt;name&gt;Red Stapler&lt;/name&gt; &lt;price currency="EUR"&gt;3.14&lt;/price&gt; &lt;/product&gt; </code></pre> <p>using either annotations, XML or YAML to tell JMSSerializerBundle how to serialize the Product-object. However, the <code>xmlns:atom</code> and <code>&lt;atom:link&gt;</code> entries should not be specified by the entity, since it should have no concept of <strong>how</strong> and <strong>where</strong> it is located. You could also imagine more links with different <code>rel</code>-attributes, such as <code>edit</code>.<br> One solution that comes to mind would be a service that listens to serialization events for specific objects, and adds these attributes and tags as appropriate. The service could use DI to get hold of the <code>Request</code>, <code>Router</code>-service etc to generate these links in a format that is suitable for the requested format. I.E in an XML-response, it could set the appropriate type to <code>application/media-format+xml</code>, whereas in a json-response, it could generate something like</p> <pre><code>"links": [ { "rel": "payment", "type": "application/media-format+json", "href": "[...]" } ] </code></pre> <p>Now, in <a href="http://jmsyst.com/bundles/JMSSerializerBundle">the documentation for JMSSerializerBundle</a>, I find annotations for <code>@PreSerialize</code>, and <code>@PostSerialize</code>, but they seem to only be able to call methods on the object being serialized.<br> Does anyone know how/if this can be achieved? Or do I have to use a templating engine such as Twig and manually create the XML-response?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload