11

I have problem with returning views with FOSRestBundle working under Symfony 4.1 Project.

This is code from my controller:

class NewsController extends FOSRestController
{

    public function getNewsAction()
    {
        $data = ['news1', 'news2'];

        $view = $this->view($data, 200);

        return $this->handleView($view);
     }
}

fos_rest.yaml

fos_rest:
    param_fetcher_listener:  true
    allowed_methods_listener:  true
    routing_loader: true
    view:
        view_response_listener:  'force'
    format_listener:
        rules:
            - { path: ^/api, prefer_extension: true, fallback_format: json, priorities: [ json, html ] }

framework.yaml

framework:
    secret: '%env(APP_SECRET)%'
    php_errors:
        log: true

sensio_framework_extra:
    view:        { annotations: true }

So I have pretty basic configuration, and I am still getting errors like this:

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

I tried to remove "view: view_response_listener: 'force'", but then I am having this error:

An instance of Symfony\Bundle\FrameworkBundle\Templating\EngineInterface >must be injected in FOS\RestBundle\View\ViewHandler to render templates.

I'am struggling with it for hours. Is it because of Symfony 4 beta status? Or maybe I am doing something wrong? Maybe I miss some dependecies? I couldn't find anything helpful about this problem in official documentation.

symfonydevpl
  • 567
  • 1
  • 6
  • 20
  • 1
    Might be related to [this](https://github.com/symfony/symfony/issues/25103) By the way, 4.1 would be the master development version. 4.0 is only in the release candidate stage. – Cerad Nov 27 '17 at 14:05
  • 3
    And according to the [bundle compatibility spreadsheet](https://docs.google.com/spreadsheets/d/1mFHQPp9uKtQTFjOBShIoPNHYgmkjHHdskdrd2C0BaF8/edit#gid=1730100865) fosrestbundle does not yet support 4.x. Not a huge surprise. – Cerad Nov 27 '17 at 14:08

4 Answers4

18

Add line to config/packages/framework.yaml

framework:
    templating: { engines: ['twig'] }

it will solve

An instance of Symfony\Bundle\FrameworkBundle\Templating\EngineInterface >must be injected in FOS\RestBundle\View\ViewHandler to render templates.

Alexander Matrosov
  • 953
  • 1
  • 11
  • 33
  • It works, but what I don't understand, that I already use twig in my project. Why do I have to add in the generic framework-config when I want to use it in the ViewHandler? – Sebus Mar 28 '18 at 15:43
12

Do you send the Accept: application/json in your request?

If not, you do not necessarily need twig, but you need to remove html from the format configuration in the bundle config:

fos_rest:
    format_listener:
        rules:
            - { path: ^/, prefer_extension: true, fallback_format: json, priorities: [ json ] }

The default is to have html in the priorities, and that requires twig.

cezar
  • 11,616
  • 6
  • 48
  • 84
magnetik
  • 4,351
  • 41
  • 58
  • 2
    Thanks! This solved the problem for me with Symfony 4.1. – cezar Oct 07 '18 at 02:45
  • This should be the correct answer! If you want to use json response for all routes, the rule {fallback_format: json} is just enough! – Alex May 19 '19 at 02:30
2

You must enable the SensioFrameworkExtraBundle view annotations

sensio_framework_extra:
    view:        { annotations: false }
Alister Bulman
  • 34,482
  • 9
  • 71
  • 110
0

Templating component integration is deprecated in Symfony 4.3. So templating section must be removed (or commented) from config/packages/framework.yaml

framework:
    # templating: { engines: ['twig'] }

To define Twig as templating service in ViewHandler add lines below to config/services.yaml

    fos_rest.templating:
        alias: twig
Vitaliy
  • 1
  • 2
  • How is this related to the question? – Nico Haase Nov 29 '19 at 09:42
  • Alexander Matrosov suggest add templating line to config to resolve last error from question (i can not add comment answers now). But it is deprecated solution in Symfony 4.3. – Vitaliy Dec 20 '19 at 13:18