Symfony2 + FOSRestBundle:为每个控制器/操作启用/禁用REST功能?

Jef*_*aes 5 rest symfony fosuserbundle fosrestbundle

我的应用程序的一部分将作为API提供,因此我的一些页面需要以JSON或XML(基于Accept标头'Content Type')提供.

我已经使用了FOSRestBundle,它运行得很好,但现在发送Accept标头'Content Type:application/xml'时,我的所有页面都以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    
Run Code Online (Sandbox Code Playgroud)

Wil*_*and 6

根据RestBundle的文档,如果您不在View控制器中使用a ,则不会获得XML输出.因此,如果您不使用@View注释或操作中的a View::create(),并且返回经典响应,则将获得HTML输出.

如果你想强制格式因为某些原因,你可以把prefer_extensiontrue和调整路由定义:

my_route:
    pattern:  /my-route
    defaults: { _controller: AcmeDemoBundle:action, _format: <format> }
Run Code Online (Sandbox Code Playgroud)

<format>您要强制使用的格式在哪里.