6

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).

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.

My setup:

//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'
        );
    }
}

My API should return XML oder JSON based on the accept header. There will never be an html output.

sprain
  • 7,552
  • 6
  • 35
  • 49

2 Answers2

28

I fixed it! The config needs to look like this:

sensio_framework_extra:
    view:
        annotations: false

fos_rest:
    view:
        view_response_listener: true
sprain
  • 7,552
  • 6
  • 35
  • 49
  • 1
    I think, it should be added in the official docs (please contact them and submit it). Because it doesn't exist up today. Congrats! – Felix Aballi May 16 '14 at 15:58
  • 1
    Just submitting related error message from logs, so that people can find this via google: `PHP message: PHP Fatal error: Uncaught exception 'RuntimeException' with message 'You need to disable the view annotations in SensioFrameworkExtraBundle when using the FOSRestBundle View Response listener.' in /home/jupiter/symfony/dimsym/vendor/friendsofsymfony/rest-bundle/FOS/RestBundle/DependencyInjection/Compiler/ConfigurationCheckPass.php:27` – Dimitry K Sep 02 '14 at 16:58
4

I spending a day for searching working configuration:

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
Stas
  • 41
  • 1
  • 1