Search code examples
phpjsonsymfonyfosrestbundle

FOSRestBundle specify default format on custom route as JSON


Having problems with FOSRestBundle custom routes, i followed the below link but that didn't solve my problem. My problem is that when i don't specify a format it defaults to XML not JSON.

How to specify default format for FOS\RestBundle to json?

Config.yml

fos_rest:
routing_loader:
    default_format: json
    include_format: true
param_fetcher_listener: force
body_listener: true
allowed_methods_listener: true
view:
    view_response_listener: 'force'
    formats:
        json: true
        xml: true
format_listener:
    rules:
        - { path: '^/api', fallback_format: json, priorities: ['json', 'xml'], prefer_extension: true }

routing.yml

get_forms:
pattern:  /api/v4/forms.{_format}
defaults: { _controller: NmpoloRestBundle:Form:cget, _format: ~ }

Does anyone know why my default goes to XML when i hit: www.test.com/api/v4/forms

its XML format not JSON. Any help would be appreciated. thx


Solution

  • I managed to default to json by placing the following in app/config/config.yml

    fos_rest:
        //...
        routing_loader:
            default_format: json
    

    My complete config was

    fos_rest:
        serializer:
            serialize_null: true
        routing_loader:
            default_format: json
        format_listener: true
        view:
            view_response_listener: 'force'
    

    No extra configuration in routing files was necessary