Symfony 4:服务显示在控制台中,但不是自动注入

问题描述 投票:1回答:1
hett@hett-pc:/data/projects/graylist$ ./bin/console  debug:container | grep client_manager
  fos_oauth_server.client_manager                alias for "fos_oauth_server.client_manager.default"                                     
  fos_oauth_server.client_manager.default        FOS\OAuthServerBundle\Document\ClientManager   

我可以在我的命令中使用该服务:

class TestCommand extends Command
{
    protected static $defaultName = 'test';
    private $clientManager;

    public function __construct(?string $name = null, \FOS\OAuthServerBundle\Document\ClientManager $clientManager)
    {
        $this->clientManager = $clientManager;
        parent::__construct($name);
    }
}

但得到错误

无法自动装配服务“App \ Command \ TestCommand”:方法“__construct()”的参数“$ clientManager”引用类“FOS \ OAuthServerBundle \ Document \ ClientManager”但不存在此类服务。它不能自动注册,因为它来自di 不同的根名称空间。

为什么?

以及如何将默认服务与ClientManagerInterface相关联?

PS:我也尝试注射ClientManagerInterface,但得到同样的错误。

symfony symfony4
1个回答
0
投票

Opps,我发现答案比写问题更快:

此服务不支持自动装配,需要在services.yaml下面添加以下行:

services:
    FOS\OAuthServerBundle\Model\ClientManagerInterface: '@fos_oauth_server.client_manager.default'
© www.soinside.com 2019 - 2024. All rights reserved.