11

My corresponding configuration is

fos_rest:
    view:
        view_response_listener: force

sensio_framework_extra:
    view:
        annotations: false

and it really annoys to specify the route as

@Route("/jobs", defaults={ "_format" = "json" })

every time.

So is it possible to specify it somewhere to be assumed by default?

PS:

If I remove defaults={ "_format" = "json" } and call the /jobs endpoint I'm getting an exception

Unable to find template "APIBundle:Jobs:post.html.twig".

PPS:

routing_loader:
    default_format: json

won't work because it's only used for automatic routes generation.

3 Answers 3

21

The final answer is much easier and is irrelevant to FOS\RestBundle:

api:
    resource: "@APIBundle/Controller/"
    type:     annotation
    defaults: {_format: json} # <<<<<<<
    prefix:   /api/
1
  • @Mateo Tibaquirá: yes
    – zerkms
    Commented Oct 2, 2014 at 19:44
11

You may specify a default_format that the routing loader will use for the _format parameter if none is specified.

# app/config/config.yml
fos_rest:
    routing_loader:
        default_format: json

By default, routes are generated with {_format} string. If you want to get clean urls (/jobs instead /jobs.{_format}) then all you have to do is add some configuration:

# app/config/config.yml
fos_rest:
    routing_loader:
        include_format:       false

Have a look at the FOSRestBundle documentation for more informations.

3
  • It will only work if I use automatic route generation. With custom routes it doesn't work. Sorry, not an answer.
    – zerkms
    Commented Oct 16, 2013 at 7:41
  • It works fine for me. With @Route('/foo') in my controller, calling /foo doesn't get any exception.
    – Picoss
    Commented Oct 16, 2013 at 7:57
  • Is the format accepted as json eventually? If you remove that parameter - does the behaviour change?
    – zerkms
    Commented Oct 16, 2013 at 8:01
9

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

3
  • I actually have seen it but didn't try yet. I will try tomorrow and will ping you with results. Thanks :-) But I already see a drawback - that way you have to pass application/json in the Accept header, while I want that json was just a format for every request unconditionally.
    – zerkms
    Commented Oct 16, 2013 at 7:56
  • 1
    ... but after reading through that page thoroughly - I think that it might work. I'm sure it will. Anyway I will let you know tomorrow
    – zerkms
    Commented Oct 16, 2013 at 8:10
  • Thanks for participation, I've found the real answer :-)
    – zerkms
    Commented Oct 16, 2013 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.