Symfony 3.4在yaml服务配置中配置时不设置参数
我使用Symfony 3.4,并尝试将Silex应用程序迁移到它。所以我不能使用Symfony的自动布线 我的服务.yml看起来像Symfony 3.4在yaml服务配置中配置时不设置参数,symfony,service,yaml,symfony-3.4,Symfony,Service,Yaml,Symfony 3.4,我使用Symfony 3.4,并尝试将Silex应用程序迁移到它。所以我不能使用Symfony的自动布线 我的服务.yml看起来像 services: # default configuration for services in *this* file _defaults: # automatically injects dependencies in your services autowire: false # automatically registers your serv
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'
编译的cach类如下所示:
$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
错误是正确的。因为缓存的类创建者没有提供我在配置中设置的参数
有人知道为什么不在生成的缓存中设置参数吗?从中,您可能有:
audit.persister.base:
class: MyBundle\Security\Audit\Persister\ChainedEntityTrailPersister
calls:
- method: 'addPersister'
arguments:
- '@audit.persister_elasticsearch'
参数
结尾带有一个s
。在Symfony中,通常在一行中写入调用:
services:
audit.persister.base:
class: MyBundle\Security\Audit\Persister\ChainedEntityTrailPersister
calls:
- ['addPersister', ['@audit.persister_elasticsearch']]
此外,您还可以将PHPStorm与一起用于自动完成。多亏了这一点,您才免于输入错误,它基本上是为您编写的:)提示:缓存已清除。Thx这有帮助,但现在我在一个需要此服务的服务中使用了相同的服务:$a=new\MyBundle\Security\Audit\Persister\ChainedEntityRailPersister()$a->addPersister();为什么不把它考虑进去?我不明白。如果您想在aother中使用此服务,您必须在其他服务的构造函数中添加反依赖项,这是在service.yml中完成的,如
orm.event\u manager.base:factory:'MyBundle\orm\factory\ormeventmanager proto:createBase'类:条令\Common\EventManager参数:-'@orm.audit.handler'-'@orm.audit.changeset\u normalizer'-“@validation\u manager'-'@annotations.reader'-'@acl\u manager'-'@property\u accessor'-'@services.propertyExpansion'-'@Doctrine\Common\Cache\ChainCache'-'@audit.persister.base'
对不起,我不知道如何格式化itI现在在工厂中注入服务容器并直接从那里获取它。。。。这是可行的,但它不能与工厂的组合。嗨,谢谢你的重播。你在哪里有关于公约的这些信息。当您查看这些文档时,会发现它们与我使用的文档相同。您好,文档!=惯例。我会在Github上寻找Symfony+bundles中的代码:我使用Symfony 5年,制作bundle,在它上面的应用程序,但我从未看到在Yaml配置中明确使用“参数”。Tanks为您提供帮助,欢迎!如果您将更大的代码块(5个多小时的工作)从Silex迁移到Symfony,您可以使用Rector来完成()