userfrosting / UserFrosting

Modern PHP user login and management framework
https://www.userfrosting.com
Other
1.64k stars 366 forks source link

Change .env UF_MODE not working #1177

Closed josemachado94 closed 3 years ago

josemachado94 commented 3 years ago

When i change UF_MODE to production, erros are still displayed and assets-raw used.

however looking for some troubleshooting, i found that:

changing $mode = getenv('UF_MODE') ?: "" to $mode = env('UF_MODE', '') on CORE Sprinkle ServiceProvider, line 244, resolves the problem.

is that save to use in production? uncomment and use env() instead of getenv() ?

josemachado94 commented 3 years ago

Possible fix (getenv() was deprecated and changed to $_ENV[] ):

Change: $mode = getenv('UF_MODE') ?: "" To: $mode = array_key_exists("UF_MODE", $_ENV) ? $_ENV['UF_MODE'] : "default";

lcharette commented 3 years ago

Which version of UserFrosting are you using?

There was an issue with using env there, not sure if it can be fixed yet : https://github.com/userfrosting/UserFrosting/blob/c92bf0b14f43cd53771d782f03c7b61c996ce814/app/sprinkles/core/src/ServicesProvider/ServicesProvider.php#L243

josemachado94 commented 3 years ago

I'm using the latest version 4.6.

If i change that line to $mode = array_key_exists("UF_MODE", $_ENV) ? $_ENV['UF_MODE'] : "default"; it works properly.

That's what is in the phptdotenv documentation:

"Putenv and Getenv 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'];"


$dotenv = Dotenv\Dotenv::createImmutable(DIR); $dotenv->load();

The above example will write loaded values to $_ENV and putenv, but when interpolating environment variables, we'll only read from $_ENV. Moreover, it will never replace any variables already set before loading the file.

By means of another example, one can also specify a set of variables to be allow listed. That is, only the variables in the allow list will be loaded:

$repository = Dotenv\Repository\RepositoryBuilder::createWithDefaultAdapters() ->allowList(['FOO', 'BAR']) ->make();

$dotenv = Dotenv\Dotenv::create($repository, DIR); $dotenv->load();

lcharette commented 3 years ago

This has been fixed in eed5f9baeb3c45b0d9e875afb20111f45628d6b1 and v4.6.2