我正在使用 Symfony 4,并且有一个配置文件需要以App\Command\FooCommand
数组的形式注入到命令中。我在 注册了一个自定义 DI 扩展App\Kernel::configureContainer()
,用于验证自定义配置文件(为了开发方便,配置很大,在开发过程中会经常更改)。该命令的构造函数是public function __construct(Foo $foo, array $config)
,我期望配置作为第二个参数。
现在我如何把这个配置放在那里?文档中提到了参数,但它不是参数。我正在考虑更改命令的定义并在Extension::load
方法中添加此参数,如下所示:
class FooExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$configuration = $this->getConfiguration($configs, $container);
$config = $this->processConfiguration($configuration, $configs);
//inject the configuration into the command
$fooCmdDef = $container->getDefinition(FooCommand::class);
$fooCmdDef->addArgument($config);
}
}
但它以错误结束
您请求了一个不存在的服务“App\Command\FooCommand”。
但是,该命令必须已自动注册为服务。
我在做什么错以及如何注入此配置?