【发布时间】:2014-10-16 19:47:40
【问题描述】:
这方面有很多话题,但我找到的答案都不适合我。
我想使用 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
我使用 symfony 2.5 和 FOSRestBundle 1.4.2。
问候
【问题讨论】:
标签: symfony fosrestbundle