Symfony 4:服务显示在控制台中但不自动注入
Symfony 4: services are shown in the console but not autoinject
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);
}
}
但是报错
Cannot autowire service "App\Command\TestCommand": argument
"$clientManager" of method "__construct()" references class
"FOS\OAuthServerBundle\Document\ClientManager" but no such service
exists. It cannot be auto-registered because it is from a di
fferent root namespace.
为什么?
以及如何将默认服务与 ClientManagerInterface
相关联?
PS:我也尝试注入 ClientManagerInterface
,但得到了同样的错误。
哎呀,我找到答案比写问题还快:
此服务不支持自动装配,需要在下面添加一行 services.yaml
:
services:
FOS\OAuthServerBundle\Model\ClientManagerInterface: '@fos_oauth_server.client_manager.default'
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);
}
}
但是报错
Cannot autowire service "App\Command\TestCommand": argument "$clientManager" of method "__construct()" references class "FOS\OAuthServerBundle\Document\ClientManager" but no such service exists. It cannot be auto-registered because it is from a di
fferent root namespace.
为什么?
以及如何将默认服务与 ClientManagerInterface
相关联?
PS:我也尝试注入 ClientManagerInterface
,但得到了同样的错误。
哎呀,我找到答案比写问题还快:
此服务不支持自动装配,需要在下面添加一行 services.yaml
:
services:
FOS\OAuthServerBundle\Model\ClientManagerInterface: '@fos_oauth_server.client_manager.default'