gpt4 book ai didi

rest - Symfony2 + FOSRestBundle : Enable/disable REST functionality per controller/action?

转载 作者:行者123 更新时间:2023-12-04 17:53:35 26 4
gpt4 key购买 nike

我的应用程序的一部分将作为 提供API ,所以 我的一些页面 需要以 JSON 或 XML 格式提供(基于 Accept header “内容类型”)。

我用过 FOSRestBundle 它工作得很好,但现在 所有 发送 Accept header “内容类型:应用程序/xml”时,我的页面在 XML(或 JSON)中可用。

因此,我想为我的某些 Controller /操作启用/禁用此功能。使用注释来做到这一点是我的理想选择。

那可能吗?

我的 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

最佳答案

根据 the RestBundle's documentation ,如果您不使用 View,您将不会获得 XML 输出。在您的 Controller 中。所以,如果你不使用 @View注释,或 View::create()在您的操作中,您返回一个经典响应,您将获得一个 HTML 输出。

如果因为某些原因要强制格式化,可以转prefer_extensiontrue并调整路由定义:

my_route:
pattern: /my-route
defaults: { _controller: AcmeDemoBundle:action, _format: <format> }

哪里 <format>是您要强制的格式。

关于rest - Symfony2 + FOSRestBundle : Enable/disable REST functionality per controller/action?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11884104/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com