6

The getenv() always returns false. I am using Symfony dotenv library and loading my variables from a .env file in the root of my project.

use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\Dotenv\Exception\PathException;

if (!getenv('APP_ENV')) {
    try {
        (new Dotenv())->load(__DIR__ . '/../.env');
    } catch (PathException $ex) {
        echo $ex->getMessage();
        exit(1);
    }
}

var_dump(getenv('APP_ENV')); // bool(false)

But when I dump the super global I can see my variables

var_dump($_ENV); // array:1["APP_ENV" => "dev"]

So what am I missing?

Andrew
  • 101
  • 1
  • 1
  • 7

4 Answers4

14

I m not using symfony but i've encountered the some problem i am using the vlucas library this is my first code that caused the problem

define('BASE_PATH',realpath(__DIR__.'/../../'));
require_once __DIR__.'/../../vendor/autoload.php';
$dotEnv = Dotenv\Dotenv::createImmutable(BASE_PATH);
$dotEnv->load();
$appName=$_ENV['APP_NAME'];
$appName2=getenv('APP_NAME');

var_dump($appName) // return "This is my website";
var_dump($appName2) // return false;

i didn't knwo the problem at first but it seems that it was because putenv() and getenv() are not thread safe

so i changed it to this code

define('BASE_PATH',realpath(__DIR__.'/../../'));
require_once __DIR__.'/../../vendor/autoload.php';
$dotEnv = Dotenv\Dotenv::createUnsafeImmutable(BASE_PATH);// <======== :) look here
$dotEnv->load();
$appName=$_ENV['APP_NAME'];
$appName2=getenv('APP_NAME');

var_dump($appName) // return "This is my website";
var_dump($appName2) // return "This is my website";

i hope this resolves your issue

Bellash
  • 7,560
  • 6
  • 53
  • 86
Ismail Bouaddi
  • 165
  • 1
  • 9
8

For Symfony 5.x+, in public/index.php

replace

(new Dotenv())->bootEnv(dirname(__DIR__).'/.env');

with

(new Dotenv())->usePutenv()->bootEnv(dirname(__DIR__).'/.env');

This worked for me.

Yogesh Salvi
  • 1,177
  • 2
  • 10
  • 17
4

By default, Symfony doesn't use putenv() (I think it's for security reasons, don't remember exactly) so you are not able to use directly getenv() if you are using Symfony's "fake" environment variables.

The best solution in my opinion is to use dependency injection instead. You can access env vars in your Symfony configuration. For example with a yaml configuration file:

framework:
    secret: '%env(APP_SECRET)%'

If you want to be able to use getenv() anyway, that I don't recommand for multiple reasons, you can do this :

  • Before Symfony 5.1 : In your config/bootstrap.php file -> new Dotenv(true)
  • Symfony 5.1 and later : public/index.php, add the following before Dotenv instantation-> Dotenv::usePutenv();

EDIT :

  • Using the putenv PHP function is not thread safe, that's why this setting defaults to false.
  • Didn't notice in the first place your using the Dotenv component as a standalone library, so you can ignore my advice concerning the dependency injection.
Florian Hermann
  • 750
  • 6
  • 15
0

The reason is that using getenv() and putenv() is strongly discouraged due to the fact that these functions are not thread safe, however it is still possible to instruct PHP dotenv to use these functions. Instead of calling Dotenv::createImmutable, one can call Dotenv::createUnsafeImmutable, which will add the PutenvAdapter behind the scenes. Your environment variables will now be available using the getenv method, as well as the super-globals:

$s3_bucket = getenv('S3_BUCKET');
$s3_bucket = $_ENV['S3_BUCKET'];
$s3_bucket = $_SERVER['S3_BUCKET'];
Sina Eft
  • 11
  • 1