FOSRestBundle: How to remove {_format} parameter?
Asked Answered
C

1

10

I need to support only single API format which is JSON and I don't like to {_format} in my routes. Is it possible to remove it?

Cotton answered 22/11, 2013 at 15:40 Comment(0)
H
17

In your config.yml, make sure you have this configured:

fos_rest:
    format_listener: true
    routing_loader:
        default_format: json
        include_format: false

Hope that helps

EDIT:

There is an example in the FOSRestBundle Docs that shows how to use the ClassResourceInterface. The biggest difference is that you don't have to manually define your routes at all. The interface will generate your routes based on you class name and the method name. So it is very important what you name your methods (you can override how the class name is used, this is shown in the docs)

for example, something like this:

use FOS\RestBundle\Routing\ClassResourceInterface {

class UserController implements ClassResourceInterface {

    public function cgetAction() {
        //return a list of all users
    }
}

would generate a route that looks like this: [GET] /users. This is how I use the bundle, and it works great. I also don't have to use the {_format} option anywhere because I don't have to define the routes manually anywhere.

note - see my original answer as well, I made an edit that might also help with how you are using the bundle. I haven't tried using the bundle the way you are, so I'm not sure if this will work or not, but the docs make it seem like it will work.

Hairraising answered 22/11, 2013 at 18:3 Comment(2)
No, this doesn't work. I was able to achieve it by removing type: rest from routing.yml and using annotations like @Rest\Get("/user") in a controller.Cotton
I guess it must depend on how you are using the bundle. I am using ClassResourceInterface and this works fine.Hairraising

© 2022 - 2024 — McMap. All rights reserved.