FOSRestBundle: How to remove {_format} parameter?

后端 未结 1 730
清酒与你
清酒与你 2021-02-20 15:48

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?

相关标签:
1条回答
  • 2021-02-20 15:57

    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.

    0 讨论(0)
提交回复
热议问题