【问题标题】:SensioFrameworkExtraBundleSensioFrameworkExtraBundle
【发布时间】:2017-10-01 18:30:49
【问题描述】:

我正在使用 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

bundle 已经安装并添加到 AppKernel.php 文件中

有什么可以帮助我的吗?

谢谢

【问题讨论】:

  • 您发布的配置文件清楚地显示注释设置为 false?

标签: php symfony fosrestbundle


【解决方案1】:

删除 sensio_framework_extra 的配置:

sensio_framework_extra:
    view:    { annotations: false }

因为默认配置是annotations: true(可以看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) 中激活序列化器的注解

我建议你试试这个配置:

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

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\Api\Controller\MyController

【讨论】:

  • 序列化器的注解已经启用。这是有效的,但我不能在我的控制器中使用 View 注释并且我有一条错误消息:“类看起来不像控制器类”
  • 您的控制器必须扩展 FOS\RestBundle\Controller\FOSRestController documentation
  • 他扩展了 FOS\RestBundle\Controller\FOSRestController,我编辑帖子添加了这部分
  • 您是否将route of type rest 添加到 app/config/routing.yml 中?
  • 是的,我看到了 php bin/console debug:router 命令和更新后的路由
【解决方案2】:

将以下代码复制到您的 main services.yml

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

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

【讨论】:

    【解决方案3】:

    mentioned source 的另一个解决方案:

    bundles.php 中,Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle 必须位于FOS\RestBundle\FOSRestBundle 之后才能正常工作

    【讨论】:

      【解决方案4】:

      使用 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 }
      

      【讨论】: