2013-04-17 58 views
6

我正在尝试关注如何使用Symfony2设置一个良好的REST API Will Durand's tutorial。不过,我在一开始我没有,因为我得到这个错误:控制器必须返回一个响应,数组给出

The controller must return a response (Array(welcome => Welcome to my API) given). 

一些基本的东西肯定有问题我非常基本的配置。我为fos_rest配置尝试了不同的设置,但configuration reference没有提供很大帮助,因为我不太了解单个设置的功能。

我的设置:

//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输出。

回答

28

我修好了! 的配置需要看起来像这样:

sensio_framework_extra: 
    view: 
     annotations: false 

fos_rest: 
    view: 
     view_response_listener: true 
+1

我认为,应该在官方的文档中添加(请与他们联系,并提交) 。因为它今天不存在。恭喜! –

+1

只需提交来自日志的相关错误消息,以便人们可以通过谷歌找到这个:'PHP message:PHP致命错误:未捕获异常'RuntimeException'带消息'您需要在使用FOSRestBundle View Response侦听器时禁用SensioFrameworkExtraBundle中的视图注释“。 in /home/jupiter/symfony/dimsym/vendor/friendsofsymfony/rest-bundle/FOS/RestBundle/DependencyInjection/Compiler/ConfigurationCheckPass.php:27' –

4

我花了一天的搜索工作配置:

sensio_framework_extra: 
    view: { annotations: false } 
    router: { annotations: true } 

fos_rest: 
    param_fetcher_listener: true 
    body_listener: true 
    format_listener: true 
    view: 
     view_response_listener: 'force' 
     formats: 
      xml: true 
      json : true 
     templating_formats: 
      html: true 
     force_redirects: 
      html: true 
     failed_validation: HTTP_BAD_REQUEST 
     default_engine: twig 
    routing_loader: 
     default_format: json 
相关问题