如何将 FOS\RestBundle 的默认格式指定为 json? [英] How to specify default format for FOS\RestBundle to json?

查看:24
本文介绍了如何将 FOS\RestBundle 的默认格式指定为 json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对应的配置是

fos_rest:
    view:
        view_response_listener: force

sensio_framework_extra:
    view:
        annotations: false

把路线指定为真的很烦

@Route("/jobs", defaults={ "_format" = "json" })

每次.

那么是否可以在默认情况下指定它?

So is it possible to specify it somewhere to be assumed by default?

附注:

如果我删除 defaults={ "_format" = "json" } 并调用 /jobs 端点,我会收到异常

If I remove defaults={ "_format" = "json" } and call the /jobs endpoint I'm getting an exception

Unable to find template "APIBundle:Jobs:post.html.twig".

PPS:

routing_loader:
    default_format: json

不起作用,因为它仅用于自动生成路由.

won't work because it's only used for automatic routes generation.

推荐答案

最终答案要简单得多,与 FOS\RestBundle 无关:

The final answer is much easier and is irrelevant to FOS\RestBundle:

api:
    resource: "@APIBundle/Controller/"
    type:     annotation
    defaults: {_format: json} # <<<<<<<
    prefix:   /api/

这篇关于如何将 FOS\RestBundle 的默认格式指定为 json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆