在Symfony4中将接口作为参数的Autowire类

时间:2019-03-18 10:47:30

标签: php symfony

给出这样的课程<IfModule mod_rewrite.c> <IfModule mod_negotiation.c> Options -MultiViews -Indexes </IfModule> RewriteEngine On # Handle Authorization Header RewriteCond %{HTTP:Authorization} . RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] # Redirect Trailing Slashes If Not A Folder... RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} (.+)/$ RewriteRule ^ %1 [L,R=301] # Handle Front Controller... RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L]

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;
    }

    ...

}

,然后将其添加到我的/** * @Route("/_exp/_exp", name="exp") */ public function expAction(Publisher $publisher) { ... }

services.yaml

但是我得到一个错误:

  

无法实例化接口Symfony \ Component \ Messenger \ MessageBusInterface

是与MessageBusInterface相关,还是我在自动装配上做错了什么。我遵循了The Symfony docs for autowiring,它们似乎一样吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

我相信MessageBusInterface服务已由Symfony Messenger组件声明。 尝试从Symfony\Component\Messenger\MessageBusInterface: ~中删除services.yaml,否则,您将覆盖默认定义。

澄清说明:MessageBusInterface服务实际上并不存在,它是“默认总线”服务的别名。您可以声明其他总线,请参见documentation