本文介绍了FOSRestBundle:如何删除 {_format} 参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只需要支持单一的 API 格式,即 JSON,我不喜欢在我的路由中使用 {_format}.可以删除吗?

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?

推荐答案

在你的 config.yml 中,确保你有这个配置:

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

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

希望有帮助

FOSRestBundle Docs 展示了如何使用 ClassResourceInterface.最大的区别是您根本不必手动定义路由.该接口将根据您的类名和方法名生成您的路由.因此,为方法命名非常重要(您可以覆盖类名的使用方式,这在文档中显示)

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)

例如,像这样:

use FOS\RestBundle\Routing\ClassResourceInterface {

class UserController implements ClassResourceInterface {

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

将生成如下所示的路由:[GET]/users.这就是我使用捆绑包的方式,而且效果很好.我也不必在任何地方使用 {_format} 选项,因为我不必在任何地方手动定义路由.

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.

注意 - 也请参阅我的原始答案,我进行了编辑,这也可能有助于您使用捆绑包的方式.我还没有尝试像你一样使用这个包,所以我不确定这是否可行,但是 docs 让它看起来可以工作.

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.

这篇关于FOSRestBundle:如何删除 {_format} 参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 07:14
查看更多