Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony local web server not detecting env variable

Tags:

symfony

I'm using symfony's local web server and whenever I use it, it doesn't seem to detect all of the environment variables in the .env file and I'm lost on why that is.

This is what I see when I load the website:

error screen

if I type the command

php bin/console debug:dotenv

I see everything I expect.

I'm lost on why this isn't working and hoping someone can help me understand what I'm possibly doing wrong.

like image 378
Eman Avatar asked Nov 15 '25 21:11

Eman


2 Answers

I did a bit more research on this because I was confused by the fact that $_ENV had the variables but getenv did not. There is a discussion on this topic with more details. Bottom line is that using putenv/getenv is not threadsafe and can cause problems for certain Apache configurations.

However it is possible to tell Symfony to use putenv assuming you accept the risk. It is part of the runtime component.

Updating composer.json will do the trick.

    "extra": {
        "runtime": {
            "use_putenv": true
        },

Be sure to run composer dump-autoload after editing composer.json so your changes get reflected in vendor/autoload_runtime.php

Needless to say you should still be injecting your variables but if you really need to use getenv (perhaps legacy code) then this is how you can do it.

like image 108
Cerad Avatar answered Nov 17 '25 10:11

Cerad


My guess is that since you're not referencing the env variables in your Symfony config anywhere, they aren't loaded into the container configuration which is why they aren't listed when using the debug:container --env-vars command.

Try adding a parameter to your services.yaml that references one of the "missing" env variables then recheck debug:container --env-vars to see if it shows up. Something like:

# services.yaml
parameters:
    app.foo: '%env(MISSING_ENV_VAR)%'

Symfony docs: listing environment variables

like image 24
Adam Richard Avatar answered Nov 17 '25 11:11

Adam Richard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!