在这个问题上有很多话题,但我发现没有一个答案对我有用。
我想使用FOSRestBundle来构建一个返回JSON的API (目前,我将来可能会添加XML )。
当我向我的路线提出请求时,我有以下例外:
[
{
message: "Unable to find template "".",
class: "InvalidArgumentException",
...我有一个控制器,它的路径是动态构建的。我配置了json格式,并返回一个具有数据数组的View,以便在json中序列化。如果不使用FOSRestBundle,我会返回一个JSONResponse并完成它,但是正如我所说的,我最终会在将来添加其他格式,所以我想以正确的方式做事情。
这是我的路线:
categories:
type: rest
resource: Certiz\Bundle\ExamBundle\Controller\CategoryController
prefix: /api
requirements:
_format: "json"我请求以下路由:/api/类别
我的config.yml:
fos_rest:
view:
formats:
json: true
xml: false
html: false
rss: false
templating_formats:
json: true
xml: false
html: false
rss: false
view_response_listener: 'force'
routing_loader:
default_format: json
include_format: true
exception:
codes:
'Symfony\Component\Security\Core\Exception\InsufficientAuthenticationException': 401
messages:
'Symfony\Component\Security\Core\Exception\InsufficientAuthenticationException': true
jms_serializer:
metadata:
directories:
exam:
namespace_prefix: "Certiz\\Bundle\\ExamBundle"
path: "@CertizExamBundle/Resources/config/serializer"我的控制器:
public function getCategoriesAction()
{
$categories = $this
->getDoctrine()
->getManager()
->getRepository('Certiz\Bundle\ExamBundle\Entity\Category')
->getTree();
return View::create()
->setStatusCode(200)
->setData($categories)
;
} // "get_categories" [GET] /categories我在FOSRestBundle 1.4.2中使用symfony 2.5。
问候
发布于 2014-08-22 21:35:51
我发现了问题所在。在我的fos_rest config.yml中,我说json格式的响应应该由模板引擎而不是序列化程序来管理。
我将配置更改为:
fos_rest:
view:
templating_formats:
json: false发布于 2017-10-17 12:02:01
来自FOSRestBundle文档:
格式和templating_formats设置确定序列化程序和模板层分别支持哪些格式。换句话说,templating_formats中列出的任何格式都需要模板来使用模板服务进行呈现,而格式中列出的任何格式都将使用序列化程序进行呈现。对于这两个设置,false值表示禁用了给定的格式。
因此,我认为您应该将未使用的格式排除在您的配置之外,并保留真正需要的内容,以使您的conf更易读懂:
fos_rest:
view:
formats:
json: true
templating_formats:
html: truehttps://stackoverflow.com/questions/25455775
复制相似问题