I've been experimenting with using Symfony2 for a simple rest API. The rest API is available in multiple formats (xml, json). Which format is delivered is controlled by the _format parameter. Most API calls return simple arrays (encoded in json or xml) so thus far I've skipped Symfony2's view layer, outputting the response directly from the controller using createResponse.

Today I needed to include a snippet of HTML within the XML or JSON output. I wrote this HTML snippet in a PHP template and want to include it via Symfony2 views. I can't figure out how to override the format parameter so I can grab my html view, then encode it as appropriate in the controller.

To be clear, my end response will still be in XML or JSON. I'd like to do something like this:

// Controller:
$ary = array(
  'id' => $content->getId(),
'data' => $this->renderView('ApiBundle:Api:dataview', array('content' => $content)
);
return $this->renderResponse($ary);


In the above code, "renderResponse" is my own method that converts to JSON or XML based on the format. But because format is set, renderView wants a template called dataview.xml.php or dataview.json.php. Remember, I actually want to load an HTML template here. It'd be nice if I could say:

$this->renderView('ApiBundle:Api:dataview', array('content'=>$content), 'html'); // Override format

Any thoughts?

--
If you want to report a vulnerability issue on symfony, please send it to 
security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "symfony users" group.
To post to this group, send email to symfony-users@googlegroups.com
To unsubscribe from this group, send email to
symfony-users+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en

Reply via email to