我的應用程序的一部分將作爲API提供,因此我的一些頁面需要以JSON或XML(基於Accept標頭的「內容類型」)提供。Symfony2 + FOSRestBundle:啓用/禁用每個控制器/操作的REST功能?
我用FOSRestBundle和發送Accept頭「內容類型:應用程序/ XML」,當它工作得很好,但現在ALL我的網頁是在XML(或JSON)可用。
所以,我想爲我的一些控制器/操作啓用/禁用此功能。我會理想的做到這一點使用註釋。
這可能嗎?
我config.yml:
fos_rest:
view:
formats:
rss: false
xml: true
json: true
templating_formats:
html: true
force_redirects:
html: false
failed_validation: HTTP_BAD_REQUEST
default_engine: twig
view_response_listener: force
body_listener:
decoders:
json: acme.decoder.json
xml: fos_rest.decoder.xml
format_listener:
default_priorities: ['html', 'xml', 'json', '*/*']
fallback_format: html
prefer_extension: false
聽起來很不錯,但它似乎並沒有工作。 – 2012-08-30 14:44:47
它適合我。你使用了正確的'View'註解類嗎?你能提供更多信息嗎? – 2012-09-13 23:05:09
如果您使用FOS \ RestBundle \ Controller \ Annotations作爲Rest;',您的註釋應該是@Rest \ View而不是'@ View' – alexismorin 2013-08-02 17:54:44