4

I'm trying to create custom handler for serializing response into csv format.

So far I've been following the steps from documentation (http://symfony.com/doc/master/bundles/FOSRestBundle/2-the-view-layer.html#custom-handler), however when calling my endpoint with .csv extension I still get the error:

The format "csv" is not supported for serialization.

Here's my services.yml config bit:

api.csv_handler:
    class: ApiBundle\ViewHandlers\CsvViewHandler

api.view_handler:
    parent: fos_rest.view_handler.default
    calls:
        - ["registerHandler", ["csv", ["@api.csv_handler", "createResponse"]]]

and in config.yml, under fos_rest I have this:

view:
    mime_types: { 'csv': ['text/csv','text/plain'] }
    view_response_listener: 'force'
    formats:
        json: true
        xml:  true
        csv:  true
    templating_formats:
        html: true
format_listener:
    enabled: true
    rules:
        - { path: ^/, priorities: [ json, xml, csv ], fallback_format: json, prefer_extension: false }
        - { path: ^/api/doc, priorities: [ html ], fallback_format: html, prefer_extension: false }

The createResponse method inside my CsvViewHandler class currently only contains some debug code to verify if it's working, which it isn't.

So what am I missing here?

Nicolas
  • 1,256
  • 1
  • 10
  • 22

1 Answers1

4

You have to register your view handler in FOSRest bundle configuration:

fos_rest:
    ...
    service:
        view_handler: my_bundle.view_handler
    ...



services:
    ...
    my_bundle.my_handler:
        class: MyBundle\View\MyHandler
    my_bundle.view_handler:
        parent: fos_rest.view_handler.default
        calls:
            - ['registerHandler', [ 'my_format', ["@my_bundle.my_handler", 'createResponse'] ] ]
    ...
Ihor Burlachenko
  • 4,689
  • 1
  • 26
  • 25