本文介绍了FOSRestBundle 在 Symfony 4.1 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Symfony 4.1 项目下的 FOSRestBundle 返回视图时遇到问题.

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.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.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) 运行时异常您必须启用 SensioFrameworkExtraBundle 视图注释才能使用 ViewResponseListener.

我试图删除视图:view_response_listener:'force'",但是我遇到了这个错误:

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

必须在 FOS\RestBundle\View\ViewHandler 中注入 Symfony\Bundle\FrameworkBundle\Templating\EngineInterface 的一个实例来渲染模板.

我为此苦苦挣扎了几个小时.是不是因为 Symfony 4 beta 状态?或者我做错了什么?也许我想念一些依赖?我在官方文档中找不到有关此问题的任何帮助.

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.

推荐答案

Add line to config/packages/framework.yaml

Add line to config/packages/framework.yaml

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

它会解决

必须在 FOS\RestBundle\View\ViewHandler 中注入 Symfony\Bundle\FrameworkBundle\Templating\EngineInterface 的一个实例来渲染模板.

这篇关于FOSRestBundle 在 Symfony 4.1 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 07:14
查看更多