-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.0] Environment variables sometimes return null #8191
Comments
Duplicate. This is a known issue with php. We've discussed it here before. Feel free to search for the issue. |
For anyone else wondering why this happens: |
Is there anyone working on a fix at PHP? I can't rely on the whole .env system if it's going to randomly crap out on me. Is the only workaround to hard-code values into config files right now? |
Another workaround is to use |
The artisan command didn't work for me. The same behavior still happens. |
You mean |
So to be clear:
'env' => env('APP_ENV')
This is like death by 1001 papercuts, just one more thing to worry about. Why was this feature added |
I don't get why would you need to cache
Well, if that the case, you need a new "duplicate" configuration for your none PHP app, if you want to use Python or Go for some of the background/queue process. |
I don't need to, I'm not asking why the feature was added, I'm asking why |
Hey @tylercubell, It is still happening in 5.2 or someone solve the issue? |
Hi, I'm encountering the same issue here, no clear solution yet ? |
Please can the docs be updated, to reflect the requirement to use config:cache?! I just lost a few hours on trying to understand why env() was behaving unpredictably, which seems particularly futile now I discover it's been discussed here a few times. |
This is documented in our upgrading guide. If there are any other places you'd like to see this in the docs, we'd be very grateful if you could send a PR please. :) |
Thanks for the quick response! So wait, is the docs site in Gitbhub, is that what you mean? This page https://laravel.com/docs/5.2/configuration#determining-the-current-environment makes it sound like using .env would be a great thing to do, but even in dev I'm seeing it toggle between the values I want and null (where, presumably, default values don't exist) |
From reading "Do something with dotenv finally #13906" I understand that there is not a real solution for it on Apache and what you need to do is to run: "php artisan config:cache" in commend. I think it's a MUST part in starting laravel project docs. Thank you @GrahamCampbell for sharing with us the commend. |
same issue with me |
upgrade my php to 7.0.10 function env get null value |
This is a known issue with PHP. Make sure you run |
run |
Not clear, but cache. |
Hi, when calling \Config::get('app.env'); I'm still getting local, although I changed the env to production. I'm using a thread safe php installation. Do I need to get the unsafe version? PS: Workaround - get App:environment() from within App and pass it to the view |
|
Hi @decadence , Good luck. |
I am facing the same issue. And the sad part is, |
@milon It will do |
Doing app/Http/Kernel.php protected function bootstrappers()
{
// Remove original LoadEnvironmentVariables bootstrapper and prepend our custom version.
return array_merge([
\App\Library\Core\LoadEnvironmentVariables::class,
], array_diff($this->bootstrappers, [
\Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class,
]));
} app/Library/Core/LoadEnvironmentVariables.php namespace App\Library\Core;
use Dotenv\Dotenv;
use Dotenv\Exception\InvalidPathException;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables as OriginalLoadEnvironmentVariables;
class LoadEnvironmentVariables extends OriginalLoadEnvironmentVariables
{
public function bootstrap(Application $app)
{
if ($app->configurationIsCached()) {
return;
}
$this->checkForSpecificEnvironmentFile($app);
try {
(new Dotenv($app->environmentPath(), $app->environmentFile()))->overload();
} catch (InvalidPathException $e) {
//
}
}
} app/Library/Utilities/helpers.php function get_env($key, $default = null)
{
// First try to obtain value from superglobals and only if that fails fall back to getenv.
if (array_key_exists($key, $_ENV)) {
$value = $_ENV[$key];
}
else if (array_key_exists($key, $_SERVER)) {
$value = $_SERVER[$key];
}
else {
$value = getenv($key);
}
// If variable does not exist return default value.
if ($value === false) {
return value($default);
}
// Finally parse and return the value.
switch (strtolower($value)) {
case 'true':
case '(true)':
return true;
case 'false':
case '(false)':
return false;
case 'empty':
case '(empty)':
return '';
case 'null':
case '(null)':
return;
}
if (strlen($value) > 1 && Str::startsWith($value, '"') && Str::endsWith($value, '"')) {
return substr($value, 1, -1);
}
return $value;
} Then it is all about replacing default It's good enough for my needs. I used it for some time now and haven't had any problems with env since. It works on my local dev machine on windows, but staging server didn't complain either. @GrahamCampbell do you think it is worth a pr? Of course if problem remains, last checked on |
It's now been over two years and nothing has changed. I think this whole environment variable issue is representative of Laravel and its official packages (especially Cashier and Spark). It looks elegant at first glance but when you scratch the surface you find these types of ridiculous design decisions. Why can't Laravel simply parse environment variables from a text file by itself? Why does it rely on an over-engineered solution that produces unstable results? I'm sorry but this is just plain stupid. Let the minority of users who care about server-level environment variables and understand the caveats opt-in instead of causing problems for the rest of us. Problem solved. Cost > Benefit. You can clearly see that by all the StackOverflow questions and comments on this thread by now. |
@imbrish did you try to clear the cache? |
@brexis how does it help to solve the env problem? |
This is a shame for laravel, nobody giving a clear solution only clear and config cache suggestions |
@alsilva86 what's wrong with this solution? |
Yes, to a lot of you who couldnt understand Let me give you this tip, when
editing /var/www/html/app/config.php
'key' =>
env('APP_KEY','base64:qIaPsvTOUeK2RCOmEjG4xGr2YkeRz84HjXy9uXQ65cY='),
'cipher' => 'AES-256-CBC',
Dont use just the key. Its a Key, value field
2017-12-12 11:09 GMT-02:00 llioor <[email protected]>:
… @alsilva86 <https://github.com/alsilva86> what's wrong with this solution?
php artisan config:cache
It will clear your old config and then will re-cache it.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#8191 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ALM6UwfE0OPE-LEZkLRokYelfHjR8Jloks5s_nsKgaJpZM4D1kgJ>
.
--
Andre Luiz / IT and Trading
[email protected] / +55 11 984129269
[image: Linkedin] <http://br.linkedin.com/in/bahuan>[image: skype]
<http://alosilva86>
|
@llioor what's wrong with this solution? that doesn't apply at all to lumen, where this is happening too, we can't do config:cache or something else like that |
The only thing that worked for me (on production) was
|
@iamvinny you seem to be talking about cached config not being updated when .env file is changed, then However many here experience different problem - when config is not cached multiple concurrent requests may cause env variables to be blank for some of these requests. |
run php artisan config:clear if you don't want to use the config cache else |
Code from vlucas/phpdotenv repository
If I load my local .env file with this method my code is worked. You need to specify this in boot function on laravel. You can create a middleware function with:
and after that you need to add the code to Or you can use the AppBefore middleware's
And you need to add middleware to
|
dotenv is so garbage. I still don't understand why you can't just do Why use a library for something that can literally be less than 10 lines of code? |
Hi! I had the same problem; when I enable cache (artisan config:cache) the env() helper function does not work. So I have created my own function to parse the .env file and i have replaced every call to env() function by my own function. |
@bionicmaster There is no solution for Lumen right now. The caching solution is good just for Laravel. |
Wrong... the production environment values have to be protected and u can not share them on the git repository. The solution is to cache the env values with |
Dude. Take a chill pill. There is a solution to the multi threading problem which, as already stated by others:
If you've done it right, then you wouldn't have to come on here and rant so much. Seems to me you aren't deploying or setting up your configs correctly. Laravel won't even call dotenv if you've got your configs cached correctly. |
Caching the config does not resolve the issue, as stated several times in this thread. Only an idiot would use Also, since your response, 3 more references were made to this issue. Clearly still happening, and no amount of condescending "uR jUsT dOiNg iT wRoNg" is going to help resolve the situation. |
@kjjdion I was responding to a guy who was CLEARLY doing it wrong as he had also posted on Reddit (and subsequently got his account banned) then came onto this github issue to rant about how terrible Dotenv & Laravel is, despite the fact that several people have pointed out his error and a fix for it. If this was still truly a massive issue, there would be hundreds of developers coming here with the same issue and it would of been fixed by now. With that said, if you can give steps on exactly why |
@llioor where do you put that call ? In the bootstrap.php file ? In your class making the DB call ? |
@P4Thi0ut
|
Laravel 7 is out. This is still an issue. Why does a web framework need server level environment variables when it can simply be done with PHP constants? |
If you make requests fast enough some or all environment variables return null.
To reproduce:
This bug occurs for me using WAMP 2.5 x64 (latest) on Windows 7.
The text was updated successfully, but these errors were encountered: