Symfony 包访问配置值
Symfony bundle access config value
我正在尝试使用配置值初始化服务:
<parameters>
<parameter key="the.binary">CHANGE_THIS</parameter>
</parameters>
<services>
<defaults public="true" />
<service id="TheBundle\TheService">
<argument key="$env" type="string">%kernel.environment%</argument>
<argument key="$binaryPath" type="string">%the.binary%</argument>
</service>
</services>
当我调试时:
bin/console debug:config mybinary
我可以看到配置似乎符合我的预期:
mybinary:
binary: '/opt/somewhere/binary'
如何从我的包配置中获取值到 CHANGE_THIS 所在的参数?
您通常只需从配置中提取值并修改服务定义,而不是参数:
class TheExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . "/../Resources/config"));
$loader->load('services.xml');
dump($configs);
$binary = $configs[0]['binary']; // Cheating a bit, should build a config tree
// Here is where you set the binary path
$service = $container->getDefinition(TheService::class);
$service->setArgument('$binaryPath',$binary);
}
从您的 services.xml 文件中删除 binaryPath 行。
我正在尝试使用配置值初始化服务:
<parameters>
<parameter key="the.binary">CHANGE_THIS</parameter>
</parameters>
<services>
<defaults public="true" />
<service id="TheBundle\TheService">
<argument key="$env" type="string">%kernel.environment%</argument>
<argument key="$binaryPath" type="string">%the.binary%</argument>
</service>
</services>
当我调试时:
bin/console debug:config mybinary
我可以看到配置似乎符合我的预期:
mybinary:
binary: '/opt/somewhere/binary'
如何从我的包配置中获取值到 CHANGE_THIS 所在的参数?
您通常只需从配置中提取值并修改服务定义,而不是参数:
class TheExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . "/../Resources/config"));
$loader->load('services.xml');
dump($configs);
$binary = $configs[0]['binary']; // Cheating a bit, should build a config tree
// Here is where you set the binary path
$service = $container->getDefinition(TheService::class);
$service->setArgument('$binaryPath',$binary);
}
从您的 services.xml 文件中删除 binaryPath 行。