symfony - 使用 FOSRestBundle 和 Symfony2 覆盖路由中的默认值 ".{_format}"

标签 symfony symfony-2.1 fosrestbundle

我想知道是否可以在 FOSRestBundle 方法中定义路由,这些方法指定格式,而不是在它们前面加上“点”。

例如,假设我希望执行以下操作:

 http://somesite.com/api/user/20        (renders in the default/fallback format)
 http://somesite.com/api/user/20/json   (renders in JSON)
 http://somesite.com/api/user/20/xml    (renders in XML)

如果我尝试定义如下路线:

/**
 * @Get("/user/{id}/{_format}
 */
public function getUserAction($id)
{
  // do stuff
}

我明白了:

路由模式“/api/users/{maximum}/{_format}.{_format}”不能多次引用变量名称“_format”。

这让我意识到它——据此,我假设我们在默认情况下谈论的是 FOSRestBundle 而不是 Symfony2——会自动将“.{_format}”添加到我定义的任何路由的末尾。我很惊讶!

所以现在,在我之前的示例中,它的工作方式如下:

 http://somesite.com/api/user/20        (renders in the default/fallback format)
 http://somesite.com/api/user/20.json   (renders in JSON)
 http://somesite.com/api/user/20.xml    (renders in XML)

可以肯定的是,差异很小,但是,我正在尝试移植使用此语法的遗留应用程序。我正在尝试做的事情可能吗?如果是这样,我如何才能禁止自动向每条路线添加“.{_format}”?

谢谢!

最佳答案

一年多过去了,FOSRestBundle 现在支持我在上面寻找的功能。

您可以通过以下配置控制此行为:

fos_rest:
    routing_loader:
        default_format:       ~
        include_format:       true

include_format 设置为 false 以从路由中删除格式。

您可以查看 FOSRestBundle 的扩展配置选项 here .

关于symfony - 使用 FOSRestBundle 和 Symfony2 覆盖路由中的默认值 ".{_format}",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13769719/

相关文章:

mysql - 如何在单个字段中存储多个选择值 Symfony2

php - 我应该把既不是 Controller 也不是模型的类放在 Symfony 的什么地方?

symfony - 如何在 Twig 中使用多个角色(Symfony2)

Symfony 2.1 : How to pass parameter to custom form field type

symfony - 如何序列化自定义 findBy() 方法?

php - FOSRestBundle - 仅允许 json + xml,如果没有其他请求则默认为 json

php - FOSRestBundle 和 JMS Serializer,获取 JSON 时出错

php - 将类继承与容器感知的抽象基类结合使用

symfony - FOSElasticaBundle 多重嵌套查询

symfony - 在 Doctrine 2 中更新(从反面)双向多对多关系?