我想知道是否可以在 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/