如何告诉Symfony2始终将_format = json用于某个URL模式?

Ada*_*sen 6 symfony

我希望任何/api以JSON格式开头的URL的响应.有没有办法为我的整个应用程序配置它?我正在使用Symfony2版本2.0.

Eln*_*mov 21

如果您在路由中使用注释并激活控制器routing.yml,则可以执行以下操作:

Api:
    resource: "@ApiBundle/Controller"
    type: annotation
    defaults: { _format: 'json' }
Run Code Online (Sandbox Code Playgroud)

如果只想为一个控制器设置它,请在控制器级别注释上设置它:

/**
 * @Route("/api", defaults={"_format": "json"})
 */
class ApiController 
{
}
Run Code Online (Sandbox Code Playgroud)