开发者

How do you change the default format to XML in Symfony?

开发者 https://www.devze.com 2023-01-22 06:16 出处:网络
I\'m writing a restful XML API for a university assignment, the spec requires no HTML frontend. There doesn\'t seem to be any documentation (or guessable functionality) regarding how to change the de

I'm writing a restful XML API for a university assignment, the spec requires no HTML frontend.

There doesn't seem to be any documentation (or guessable functionality) regarding how to change the default format? Whilst thus far I have created all templates as ...Success.xml.php it would be easier to just use the regular ones and set this globally; I really expected this functionality to be configurable from YAML.. yet I have found some hard coded references to the HTML format.

The main issue I'm encountering is that part of the assessment is returning a 404 in a cert开发者_StackOverflow社区ain way (not as a 404 :/), but importantly it must always return XML, and the default setup of a missing route is a HTML 404 not XML (so it only works when I use forward404 from an action running via a XML route.

So in summary, is there a way to do this / what class(es) do I have to override?


Try putting this in factories.yml

all:
  request:
    class: sfWebRequest
    param:
      default_format: xml

That will still need the template names changing though. It will just mean that urls that don't specify a format will revert to xml instead of html.

You can subclass sfPHPView and override the initialise method to affect this (copy paste the initialise method from sfView) - the lines like this need changing:

if ('html' != $format)

You then need to change the view class used ... try this:

http://mirmodynamics.com/post/2009/02/23/symfony%3A-use-your-own-View-class

0

精彩评论

暂无评论...
验证码 换一张
取 消