[英]Symfony 5.4 environmental variables in services.yaml::parameters
Recently I decided to use Symfony 5.4 container in one of the projects to provide some comprehensive DI.最近我决定在其中一个项目中使用 Symfony 5.4 容器来提供一些全面的 DI。 It works well as usual, until I tried to use some env vars in
services.yaml::parameters
section.它像往常一样运行良好,直到我尝试在
services.yaml::parameters
部分中使用一些环境变量。
Docs state that to bind to an env var I should我应该绑定到环境变量的文档 state
# services.yaml
parameters:
my_var: '%env(SOME_ENV_VAR)%'
and it will be resolved from an env var on first call.它将在第一次调用时从环境变量中解决。 Okay.
好的。 I did it this way and here what I get:
我这样做了,我得到了:
echo $container->getParameter('my_var');
// env_b057c2b619f37f36_SOME_ENV_VAR_222ed306d0932595cbdeada438ccbb2a
I do see SOME_ENV_VAR
in both $_SERVER
and $_ENV
.我确实在
$_SERVER
和$_ENV
中都看到SOME_ENV_VAR
。 I also tried Dotenv
component to be sure I'm not missing something, but vainly.我还尝试
Dotenv
组件,以确保我没有遗漏任何东西,但徒劳无功。 Any env var turns into this sort of env_{hash}_{VAR_NAME}_{hash}
pattern.任何 env var 都会变成这种
env_{hash}_{VAR_NAME}_{hash}
模式。
I'm not using complete Symfony installation, just some spare components.我没有使用完整的 Symfony 安装,只是一些备用组件。 What I'm missing?
我错过了什么? Should I manually populate each env var on container build stage?
我应该在容器构建阶段手动填充每个环境变量吗?
Container is instantiated as follows:容器实例化如下:
// $_ENV and $_SERVER already contain `SOME_ENV_VAR` here
require_once __DIR__ . '/vendor/autoload.php';
// `use` statements go here
$containerBuilder = new ContainerBuilder();
$loader = new YamlFileLoader(
$containerBuilder,
new FileLocator(implode(DIRECTORY_SEPARATOR, [__DIR__, 'config']))
);
$loader->load('services.yaml');
$containerBuilder->compile();
$container = $containerBuilder;
$my_var = $container->getParameter('SOME_ENV_VAR');
echo $my_var;
Based on your updated snippet, you need to use:根据您更新的代码段,您需要使用:
$container->compile(true); // false is the default
The argument is called resolveEnvPlaceholders.该参数称为resolveEnvPlaceholders。 I remember it catching me some time ago.
我记得前段时间它抓住了我。
Still find it a bit puzzling that you have $_ENV set but I'll take your word for it.仍然觉得你设置了 $_ENV 有点令人费解,但我会相信你的话。
This is an old test case I dug up that still works:这是我挖出的一个旧测试用例,它仍然有效:
use Symfony\Component\DependencyInjection\ContainerBuilder;
class Service
{
public function __construct(string $dsn)
{
echo $dsn . "\n";
}
}
$container = new ContainerBuilder();
$container->autowire(Service::class,Service::class)->setPublic(true)
->setArguments([
'%env(DSN)%'
]);
$container->setParameter('my_var','%env(DSN)%');
putenv('DSN=dsn_value');
$container->compile(true);
$service = $container->get(Service::class);
echo $container->getParameter('my_var') . "\n";
The signature for ContainerBuilder::compile()
is: ContainerBuilder::compile()
的签名是:
public function compile(bool $resolveEnvPlaceholders = false)
If you do not pass it true
, it won't resolve the environment variables' placeholders.如果您不传递它
true
,它将不会解析环境变量的占位符。
Additionally, there is an issue with your example.此外,您的示例存在问题。
You are calling:您正在调用:
$my_var = $container->getParameter('SOME_ENV_VAR');
But SOME_ENV_VAR
is not a container parameter, but an environment variable.但是
SOME_ENV_VAR
不是容器参数,而是环境变量。 The correct call would be:正确的调用是:
$my_var = $container->getParameter('my_var');
The whole thing would be:整个事情将是:
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
require_once __DIR__ . '/vendor/autoload.php';
$containerBuilder = new ContainerBuilder();
$loader = new YamlFileLoader(
$containerBuilder,
new FileLocator(implode(DIRECTORY_SEPARATOR, [__DIR__, 'config']))
);
$loader->load(__DIR__ . '/services.yaml');
$containerBuilder->compile(true);
$container = $containerBuilder;
$my_var = $container->getParameter('my_var');
echo $my_var;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.