【问题标题】:Symfony 3.4 dont set arguments when i configure it in yaml service configSymfony 3.4 在 yaml 服务配置中配置时不设置参数
【发布时间】:2019-04-24 22:15:27
【问题描述】:

我使用 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'

编译后的缓存类如下:

$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

错误是正确的。因为缓存的类创建者没有提供我在 config.xml 中设置的参数。

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

【问题讨论】:

  • 提示:缓存已清除。

标签: symfony service yaml symfony-3.4


【解决方案1】:

documentation,您可能有:

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

argumentss 结尾。

【讨论】:

  • 谢谢,这有帮助,但现在我有一项服务需要同样的服务:$a = new \MyBundle\Security\Audit\Persister\ChainedEntityTrailPersister(); $a->addPersister();为什么不在那里考虑?
  • 我不明白。如果你想在另一个服务中设置这个服务,你必须在另一个服务的构造函数中添加de依赖
  • 是的,这是在 service.yml 中完成的,例如 orm.event_manager.base: factory: 'MyBundle\ORM\Factory\ORMEventManagerProto:createBase' class: Doctrine\Common\EventManager arguments: - '@orm.audit.handler' - '@orm.audit.changeset_normalizer' - '@validation_manager' - '@annotations.reader' - '@acl_manager' - '@property_accessor' - '@services.propertyExpansion' - '@Doctrine\Common\Cache\ChainCache' - '@audit.persister.base'
  • 对不起,我不知道如何格式化它
  • 我现在在工厂中注入服务容器并直接从那里获取它......这可行,但它似乎不能与工厂组合一起使用。
【解决方案2】:

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

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

此外,您可以使用 PHPStorm 和 Symfony plugin 进行自动完成。多亏了这一点,你才免于拼写错误,它基本上是为你写的 :)

【讨论】:

  • 嗨,感谢重播。您在哪里可以获得有关公约的信息。当您查看symfony.com/doc/3.4/service_container/calls.html 时,我正在使用记录。
  • 嗨,文档!=约定。我会在 Symfony + bundles 中的 Github 上寻找代码:github.com/symfony/symfony/… 我使用 Symfony 5 年,在其上制作捆绑包和应用程序,但我从未在 Yaml 配置中明确使用“参数”。
  • 坦克寻求帮助
  • 不客气!如果你将更大的代码块(超过 5 个小时的工作)从 Silex 迁移到 Symfony,你可以使用 Rector 来实现(github.com/rectorphp/rector
猜你喜欢
  • 1970-01-01
  • 2015-07-06
  • 2019-02-21
  • 1970-01-01
  • 2019-11-18
  • 2019-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多