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

后端 未结 3 889
故里飘歌
故里飘歌 2021-01-11 14:20

My corresponding configuration is

fos_rest:
    view:
        view_response_listener: force

sensio_framework_extra:
    view:
        annotations: false
         


        
3条回答
  •  独厮守ぢ
    2021-01-11 15:19

    I couldn't test this solution myself but following the documentation it seems that you can use a default format by giving rules on path

    config.yml

    fos_rest:
        format_listener:
            rules:
                # setting fallback_format to json means that instead of considering
                # the next rule in case of a priority mismatch, json will be used
                -
                    path: '^/'
                    host: 'api.%domain%'
                    priorities: ['json', 'xml']
                    fallback_format: json
                    prefer_extension: false
    

    With such, a request made with Accept-headers containing

    text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,application/json
    

    Will result in a json Request format

提交回复
热议问题