当我在 yaml 服务配置中配置它时,Symfony 3.4 不设置参数

Symfony 3.4 dont set arguments when i configure it in yaml service config

我使用 Symfony 3.4,我尝试将 Silex 应用程序迁移到它。所以我不能使用 Symfony 的自动装配。

我的service.yml长得像

services:
# default configuration for services in *this* file
 _defaults:
 # automatically injects dependencies in your services
  autowire: false
  # automatically registers your services as commands, event subscribers, etc.
  autoconfigure: false
  # this means you cannot fetch services directly from the container via $container->get()
  # if you need to do this, you can override this setting on individual services
  public: false

  audit.persister.base:
   class: MyBundle\Security\Audit\Persister\ChainedEntityTrailPersister
   calls:
    - method: 'addPersister'
      argument:
       - '@audit.persister_elasticsearch'

编译后的缓存 class 看起来像:

$this->services['audit.persister.base'] = $instance = new \MyBundle\Security\Audit\Persister\ChainedEntityTrailPersister();

$instance->addPersister();

我收到错误:

 Type error: Too few arguments to function MyBundle\Security\Audit\Persister\ChainedEntityTrailPersister::addPersister(), 0 passed in /var/www/html/api/var/cache/local/ContainerAdjsiif/getAudit_Persister_BaseService.php on line 14 and exactly 1 expected

错误正确。因为缓存的 class 创建者没有提供我在配置中设置的参数。

有人知道为什么参数不会在生成的缓存中设置吗?

来自 documentation,您可能有:

  audit.persister.base:
     class: MyBundle\Security\Audit\Persister\ChainedEntityTrailPersister
     calls:
         - method: 'addPersister'
          arguments:
               - '@audit.persister_elasticsearch'

arguments 结尾有一个 s

在 Symfony 中,习惯将调用写在一行中:

services:
    audit.persister.base:
        class: MyBundle\Security\Audit\Persister\ChainedEntityTrailPersister
        calls:
            - ['addPersister', ['@audit.persister_elasticsearch']]

此外,您可以将 PHPStorm 与 Symfony plugin 一起用于自动完成。多亏了它,你才不会出现拼写错误,它基本上是为你写的:)