问题描述
我正在尝试遵循 Will Durand 的教程关于如何使用 Symfony2 设置一个好的 REST API.但是我一开始就失败了,因为我收到了这个错误:
I am trying to follow Will Durand's tutorial on how to set up a good REST API with Symfony2. However I am failing in the very beginning as I get this error:
The controller must return a response (Array(welcome => Welcome to my API) given).
我非常基本的配置肯定有一些基本的问题.我为 fos_rest
配置尝试了不同的设置,但是 配置参考并没有提供什么帮助,因为我不太了解单个设置的作用.
Something basic must be wrong with my very basic configuration. I have tried different setting for the fos_rest
config, but the configuration reference doesn't provide to be very helpful as I do not really understand what the single settings do.
我的设置:
//config.yml
sensio_framework_extra:
view:
annotations: true
fos_rest: ~
//Controller
<?php
namespace Acme\Bundle\ApiBundle\Controller;
use FOS\RestBundle\Controller\Annotations as Rest;
class DefaultController
{
/**
* @Rest\View
*/
public function indexAction()
{
return array(
'welcome' => 'Welcome to my API'
);
}
}
我的 API 应该根据接受标头返回 XML 或 JSON.永远不会有 html 输出.
My API should return XML oder JSON based on the accept header. There will never be an html output.
推荐答案
我修好了!配置需要如下所示:
I fixed it!The config needs to look like this:
sensio_framework_extra:
view:
annotations: false
fos_rest:
view:
view_response_listener: true
这篇关于控制器必须返回响应,给定数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!