Search code examples
phpsymfonyfosrestbundle

SensioFrameworkExtraBundle


I'm using Symfony 3.3 and i'm trying to use FOSRestController to make an API.

Here is my config files :

# SensioFrameworkExtra Configuration
sensio_framework_extra:
    view:    { annotations: false }

# FOSRest Configuration
fos_rest:
    format_listener:
        rules:
            - { path: '^/api', priorities: ['json'], fallback_format: 'json' }
            - { path: '^/', stop: true }
    view:
        view_response_listener: true

Controller :

<?php

namespace AppBundle\Api;

use FOS\RestBundle\Controller\Annotations as REST;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\View\View;

class MyController extends FOSRestController
{

    /**
     * @REST\Get("/some-url")
     * @REST\View()
     *
     * @return View
     */
    public function getSomethingAction()
    {
        $view = View::create();

        return $view;
    }

}

The issue is about the view_response_listener, i'm having this error message :

(1/1) RuntimeException
You must enable the SensioFrameworkExtraBundle view annotations to use the ViewResponseListener.

Routing :

api_something:
    type: rest
    resource: AppBundle\Api\MyController

The bundle is already installed and added to the AppKernel.php file

Can something help me with this ?

Thanks


Solution

  • Remove your configuration of sensio_framework_extra :

    sensio_framework_extra:
        view:    { annotations: false }
    

    Because the default configuration is annotations: true (you can look at vendor/sensio/framework-extra-bundle/DependencyInjection/Configuration.php)

    Let the default configuration of sensio_framework_extra https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/index.html#configuration

    You may have forgotten to activate the annotations of serializer in config.yml (https://symfony.com/doc/current/serializer.html#using-serialization-groups-annotations)

    I suggest you to try this configs :

    #app/config/config.yml
    framework:
        ....
        serializer: { enable_annotations: true }
    fos_rest:
        ....
        view:
            view_response_listener: 'force'
    

    Documentation of FosRestBundle view_response_listener : http://symfony.com/doc/master/bundles/FOSRestBundle/3-listener-support.html

    http://symfony.com/doc/master/bundles/FOSRestBundle/view_response_listener.html

    Try to create the directory AppBundle/Api/Controller. And put your MyController.php into it. Your controller will be named

    \AppBundle\Api\Controller\MyController