繁体   English   中英

Symfony 4 自动装配无法正常工作

[英]Symfony 4 autowiring not working properly

这是上下文:

  1. 已安装带有作曲家“作曲家需要 leezy/pheanstalk-bundle”的 beanstalk 包

  2. 我正在尝试在命令中使用,但出现此错误

无法自动装配服务“App\\Command\\Worker\\ProcessParserCommand”:方法“__construct()”的参数“$pheanstalk”引用接口“Pheanstalk\\Contract\\PheanstalkInterface”但没有这样的
服务存在。 您可能应该将此接口别名为现有的“leezy.pheanstalk.proxy.default”服务。

class ProcessParserCommand extends Command
{
    protected static $defaultName = 'app:worker:process-parser';

    /** @var PheanstalkInterface $pheanstalk */
    private $pheanstalk;

    protected function configure()
    {
        $this
            ->setDescription("Parse something")
        ;
    }



    public function __construct(PheanstalkInterface $pheanstalk)
    {
        $this->pheanstalk=$pheanstalk;

        parent::__construct();
    }
}

事实证明,这是那些具有欺骗性的错误消息之一。

通常,当您收到“接口不存在,可能是别名 SomeService”消息时,这意味着需要将接口显式定义为别名:

# config/services.yaml
Pheanstalk\Contract\PheanstalkInterface:
    alias: 'leezy.pheanstalk.proxy.default'

但是在这种情况下,虽然这样做可以让您克服接口错误,但会产生一个新的“构造函数参数太少”错误。

看一看 bundle 的文档,你需要一些配置才能真正生成一个 pheanstalk 实例。 composer require 命令足够智能,可以将包添加到您的 bundles.php 文件中,但不会创建配置文件。 因此,根据文档添加配置文件:

# config/packages/leezy_pheanstalk.yaml
leezy_pheanstalk:
    pheanstalks:
       primary:
           server: beanstalkd.domain.tld
           default: true

而且很快。 错误消失了。 作为奖励,config/services.yaml 中的别名不再需要,如果您添加它,应该将其删除。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM