首页 文章

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

提问于
浏览
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”但不存在此类服务 . 它不能自动注册,因为它来自不同的根命名空间 .

为什么?

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

PS:我也尝试注入 ClientManagerInterface ,但是同样的错误 .

1 回答

  • 0

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

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

    services:
        FOS\OAuthServerBundle\Model\ClientManagerInterface: '@fos_oauth_server.client_manager.default'
    

相关问题