FOSRestBundle doesn't work in Symfony 4.1

后端 未结 4 2048
无人共我
无人共我 2021-02-20 12:52

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

This is code from my controller:

class NewsController extends          


        
相关标签:
4条回答
  • 2021-02-20 13:00

    You must enable the SensioFrameworkExtraBundle view annotations

    sensio_framework_extra:
        view:        { annotations: false }
    
    0 讨论(0)
  • 2021-02-20 13:06

    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
    0 讨论(0)
  • 2021-02-20 13:15

    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.

    0 讨论(0)
  • 2021-02-20 13: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.

    0 讨论(0)
提交回复
热议问题