doulian8485 2018-02-01 23:43
浏览 128
已采纳

Symfony 3.3 - Services.yml参数(ResponseFactory) - 参数以字符串而不是对象的形式传递

The Error

Hello, I am getting the following error:

Type error: Argument 2 passed to ApiExceptionBundle\EventListener\ApiExceptionSubscriber::__construct() must implement interface ApiExceptionBundle\Component\Factory\ResponseFactoryInterface, string given, called in C:\htdocs\projects\myproject\var\cache\dev\appDevDebugProjectContainer.php on line 4293

The Call Stack Showing The Incorrect Parameter Type Being Passed

As can be seen below from the call stack, the second parameter is a string. (But it should actually be an object instantiated of that type).

at ApiExceptionSubscriber->__construct(true, 'ApiExceptionBundle\\Component\\Factory\\ResponseFactoryInterface', true, array('/admin/'))
in appDevDebugProjectContainer.php (line 4293)

The Constructor Definition

The subscriber is located at myproject\src\ApiExceptionBundle\EventListener\ApiExceptionSubscriber.php and its constructor definition is the following:

public function __construct($debug, ResponseFactoryInterface $responseFactory, $enabled = true, $excludedPaths = [])

(Notice above that it is expecting a ResponseFactoryInterface object rather than a string).

The Services.yml Entry

In myproject/src/ApiExceptionBundle/Resources/config/services.yml, the relevant entry is below:

ApiExceptionBundle\EventListener\ApiExceptionSubscriber:
        arguments:
            $responseFactory: 'ApiExceptionBundle\Component\Factory\ResponseFactoryInterface'

Other Things I've Tried

From the original example I am using, I see that there is actually an @ symbol at the beginning of the $responseFactory argument assignment in the services.yml file.

The original code that I am using as an example, has a services.yml entry similar to this:

$responseFactory: '@ApiExceptionBundle\Component\Factory\ResponseFactoryInterface'

Unfortunately when I try to add the @ symbol in my code (like above), it gives me this error:

PHP Fatal error:  Uncaught Symfony\Component\DependencyInjection\Exception\RuntimeException: Cannot dump de
finition because of invalid class name ('@ApiExceptionBundle\\Component\\Factory\\ResponseFactory') in C:\h
tdocs\projects\myproject\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Dumper\PhpDumper.ph
p:1568

Questions

So, I have 2 questions:

  1. What am I doing wrong? (I'm assuming it's the way I'm referencing things in services.yml).
  2. What does the @ symbol do? Taking a guess, I think it tells the code to instantiate an object of the class referenced rather than just literally using the string. Is that correct?
  • 写回答

1条回答 默认 最新

  • dpwh11290 2018-02-01 23:54
    关注

    For Symfony 3.3+

    Looks like you're trying to pass interfaces. Specific instance should be used instead.

    Have you tried autowiring with alias?

    # services.yml
    
    services:
        ApiExceptionBundle\YourResponseFactory: ~
        ApiExceptionBundle\Component\Factory\ResponseFactoryInterface:
            alias: ApiExceptionBundle\YourResponseFactory
    
        ApiExceptionBundle\EventListener\ApiExceptionSubscriber:
            autowire: true
    

    With PSR-4 autodiscovery

    Or you can use PSR-4 autodiscovery:

    # services.yml
    
    services:
        _defaults:
            autowire: true
    
        ApiExceptionBundle\: ~
            resource: ../ # path to ApiExceptionBundle directory
    
        ApiExceptionBundle\Component\Factory\ResponseFactoryInterface:
            alias: ApiExceptionBundle\YourResponseFactory
    

    Symfony 3.4+

    Since Symfony 3.4 you can ommit the alias thanks to autoaliasing for single-services from your code.

    # services.yml
    
    services:
        _defaults:
            autowire: true
    
        ApiExceptionBundle\: ~
            resource: ../ # path to ApiExceptionBundle directory
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上