Symfony2 JSON example Symfony2 JSON example symfony symfony

Symfony2 JSON example


is this a correct methodology?

Yes I like it but I would modify the routing rule a bit like this:

test:    pattern: /test.{_format}    defaults: { _controller: myProjectmyTestBundle:Message:test, _format: json}    requirements: { _format: (xml|json), _method: GET }

what do I put on the code_req_here?

Put the array that you want to convert to json format.ex. array(array('id' => 1, 'value' => 'test'), array('id' => 2, 'value' => 'smart'))


I recommend using

http://jmsyst.com/bundles/JMSSerializerBundle

$serializer = $container->get('jms_serializer');$serializer->serialize($data, 'json'); // json|xml|yml$data = $serializer->deserialize($inputStr, $typeName, $format);