SensioFrameworkExtraBundle

时间:2017-10-01 18:30:50

标签: php symfony fosrestbundle

我正在使用Symfony 3.3,我正在尝试使用FOSRestController制作API。

这是我的配置文件:

# 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

控制器:

<?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;
    }

}

问题在于view_response_listener,我收到此错误消息:

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

路由:

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

该软件包已经安装并添加到AppKernel.php文件

有什么能帮我解决这个问题吗?

由于

4 个答案:

答案 0 :(得分:3)

删除sensio_framework_extra的配置:

n

因为默认配置是sensio_framework_extra: view: { annotations: false } (您可以查看vendor / sensio / framework-extra-bundle / DependencyInjection / Configuration.php)

设置sensio_framework_extra https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/index.html#configuration

的默认配置

您可能忘记在config.yml中激活序列化程序的注释(https://symfony.com/doc/current/serializer.html#using-serialization-groups-annotations

我建议你试试这个配置:

annotations: true

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

尝试创建AppBundle / Api / Controller目录。并将你的MyController.php放入其中。您的控制器将命名为

  

\的appbundle \阿比\控制器\ myController的

答案 1 :(得分:0)

将以下代码复制到主services.yml

sensio_framework_extra.view.listener:
    alias: Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener

解决方案的来源: https://github.com/FriendsOfSymfony/FOSRestBundle/issues/1768#issuecomment-340294485

答案 2 :(得分:0)

来自mentioned source的另一个解决方案:

<{1>} bundles.php Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle必须位于FOS\RestBundle\FOSRestBundle之后才能正常工作

答案 3 :(得分:0)

使用Symfony 4.0回答相同的问题

routes / rest.yaml 路由配置

app_admin_users:
    type:     rest
    resource: App\Controller\Api\Admin\User\UsersController
    prefix: /api

fos_rest.yaml 查看响应侦听器已启用

fos_rest:
    view:
        view_response_listener: true

framework_extra.yaml 查看已启用的注释

sensio_framework_extra:
    view:        { annotations: true }

config.yaml 导入fos_rest和framework_extra

imports:
    - { resource: fos_rest.yaml }
    - { resource: framework.yaml }
    - { resource: framework_extra.yaml }