1

My goal is to have a default json output when the user agent is set to anything else than json or xml. I have configured FOSRestBundle 1.1.0 as follows:

fos_rest:
format_listener: true
param_fetcher_listener: true
view:
    default_engine: php
    formats:
        json: true
        xml: true
    templating_formats:
        html: false
    view_response_listener: force
routing_loader:
    default_format: json

This works. Now I added the following configuration:

format_listener:
    rules:
        - { fallback_format: json, prefer_extension: false, priorities: ['xml', 'json'] }

As soon as I do that, I can no longer switch between formats by either appending ?_format=json or ?_format=xml and also the Accept header seems to be ignored, it always uses whatever I specify in the accept header.

How to configure FOSRestBundle so that it accepts json or XML via Parameter/HTTP Request header and falls back to json if the format accepted by the browser is HTML?

4

1 回答 1

2

根据FOSRestBundle Docs

请注意,如果 _format 在路由内匹配,则添加一个虚拟 Accept 标头设置,其中 aq 设置比最低的 Accept 标头低一,这意味着最后检查该格式是否匹配优先级。如果 prefer_extension 设置为 true,则虚拟 Accept 标头将比最高 q 高一个,从而首先检查扩展名。将优先级设置为非空数组会启用 Accept 标头协商。

另外,我注意到在该rules部分中,您缺少该path选项。所以应用程序不知道将规则应用到哪个路径。

看看我上面链接的文档,它有一个例子。

于 2013-12-20T17:12:09.013 回答