如何从控制器外的parameters.yml中检索参数?

use*_*096 2 php symfony symfony-console

我需要从参数parameters.yml中的Command\ContainerAwareCommand类.我没有看到$this->getParameter()那里.

有没有一种从parameters.yml外部控制器检索参数的简单方法?

比以下更简单:编写"友好配置",并将参数foo放入config.yml,然后检索参数DependencyInjection\Extension::load(),并设置它:$container->setParameter('foo', $foo),最后在Command\ContainerAwareCommand检索它$this->getContainer()->getParameter('foo')

Mat*_*teo 5

您可以通过以下方式访问容器:

... extends ContainerAwareCommand

...
$this->getContainer()->getParameter('my-params');
Run Code Online (Sandbox Code Playgroud)

编辑:

您可以定义自己的自定义参数,然后在主config.yml文件中导入,例如:

config.yml

imports:
    - { resource: parameters.yml }
    - { resource: security.yml }
    - { resource: services.yml }
    - { resource: "@ApplicationBundle/Resources/config/parameters.yml" }
Run Code Online (Sandbox Code Playgroud)

希望这个帮助

  • 注意:自 Symfony 4.2 起,ContainerAwareCommand 已被@弃用,请使用 {@see Command} 代替。也可以从执行命令本身访问容器: $container = $this->getApplication()->getKernel()->getContainer(); (2认同)