[英]Autowire Class with Interface as param in Symfony4
Given a class Publisher
like this: 给定这样的班级
Publisher
:
<?php
namespace App\Util\Publisher;
use Symfony\Component\Mercure\Update;
use Symfony\Component\Messenger\MessageBusInterface;
class Publisher
{
protected $topic = null;
protected $bus;
/**
* Publisher constructor.
* @param MessageBusInterface $bus
*/
public function __construct(MessageBusInterface $bus)
{
$this->topic = getenv('TOPIC_MAIN_URL');
$this->bus = $bus;
}
...
}
I would like to autowire it in my controllers like this: 我想像这样在我的控制器中自动接线:
/**
* @Route("/_exp/_exp", name="exp")
*/
public function expAction(Publisher $publisher)
{
...
}
and I added this to my services.yaml
: 并将其添加到我的
services.yaml
:
Symfony\Component\Messenger\MessageBusInterface: ~
App\Util\Publisher\Publisher:
autowire: true
arguments: ['@Symfony\Component\Messenger\MessageBusInterface']
But I get an error: 但是我得到一个错误:
Cannot instantiate interface Symfony\\Component\\Messenger\\MessageBusInterface
无法实例化接口Symfony \\ Component \\ Messenger \\ MessageBusInterface
Is that related to the MessageBusInterface or am I doing something wrong with the autowiring. 是与MessageBusInterface有关,还是我在自动装配上做错了什么。 I followed The Symfony docs for autowiring and they seem to be the same?
我遵循了Symfony文档进行自动装配 ,它们看起来是一样的吗?
Thank you! 谢谢!
I believe MessageBusInterface
service is already declared by Symfony Messenger component. 我相信Symfony Messenger组件已经声明了
MessageBusInterface
服务。 Try to remove Symfony\\Component\\Messenger\\MessageBusInterface: ~
from your services.yaml
, otherwise you are overriding the default definition. 尝试从
services.yaml
删除Symfony\\Component\\Messenger\\MessageBusInterface: ~
,否则您将覆盖默认定义。
A note for clarification: MessageBusInterface
service does not really exists, it in an alias over the "default bus" service. 需要澄清的注释:
MessageBusInterface
服务实际上并不存在,它是“默认总线”服务的别名。 You can declare other buses, cf documentation 您可以声明其他总线,请参阅文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.