G
G
grabbee2021-07-08 15:08:54
symfony
grabbee, 2021-07-08 15:08:54

How to cross "symfony/dependency-injection" with environment variables?

I don't use a framework. Components only. The service container does not see the variable from the environment. I created a .env file - I wrote down the DSN variable (MESSENGER_TRANSPORT_DSN) there to use it in the configuration. Before creating a container, I load environment variables from a file

$dotenv = new Dotenv();
//$dotenv->usePutenv();
$dotenv->bootEnv('../.env');

But the variable does not reach the service
arguments:
            $dsn: '%env(MESSENGER_TRANSPORT_DSN)%'

Although it can be seen through getenv('MESSENGER_TRANSPORT_DSN') if you uncomment usePutenv();

Did I miss something. How to force container to see variables from .env

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BoShurik, 2021-07-08
@grabbee

https://github.com/symfony/symfony/blob/5.4/src/Sy...

/**
 * @param bool $resolveEnvPlaceholders Whether %env()% parameters should be resolved using the current
 *                                     env vars or be replaced by uniquely identifiable placeholders.
 *                                     Set to "true" when you want to use the current ContainerBuilder
 *                                     directly, keep to "false" when the container is dumped instead.
 */
public function compile(bool $resolveEnvPlaceholders = false)

Apparently you are not dumping the container. You need to call
$container->compile(true);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question