Let's say I have one REST API call:
/api/message/list
I want to have different response format depending on HTTP Accept Header.
It works perfect when I request for JSON or XML from curl command. But when I load it from browser, it tries to render twig template and I do NOT want that. I want it returns XML content instead of HTML. With this approach I don't need to create a twig file for browser output.
I want to do this because is practical to have the API output on the browser without need to create a twig for it.
Am I missing something in the configuration file? Or maybe this is not currently supported by default?
I tried using this but doesn't work.
fos_rest:
body_listener:
decoders:
html: fos_rest.decoder.xml
...
My full FOSRest config on config.yml:
fos_rest:
param_fetcher_listener: true
body_listener:
decoders:
json: fos_rest.decoder.json
xml: fos_rest.decoder.xml
html: fos_rest.decoder.xml
format_listener:
default_priorities: [html, xml, json]
fallback_format: xml
view:
view_response_listener: force
formats:
json: true
xml: true
force_redirects:
html: true
failed_validation: HTTP_BAD_REQUEST
default_engine: php
sensio_framework_extra:
view: { annotations: false }
router: { annotations: true }
My list action on controller returns just the array of messages:
<?php
use FOS\RestBundle\Controller\Annotations as Rest;
class MessageController extends Controller {
/**
* @Rest\View()
*/
public function listAction() {
$messages = //get the messages
return $messages;
}
...
}
?>
Thanks!