Comments (19)
Hmm, but then you have a chicken-egg problem: In dev mode you would use Dotenv to set env vars, so you can't set APP_ENV
in the first place.
I really can't see the drawback if Dotenv would be more relaxed in regard to .env
: If it's there, load it. If not - ignore anything Dotenv related.
Of course I can add a if (file_exists('.env'))
- it's just some extra inconvenience. Maybe I see something in Dotenv that it doesn't want to be: A transparent drop-in replacement for environment vars. It should only do something if .env
exists - and stay out of the way if no such file is present.
from phpdotenv.
@vlucas Maybe I have a big misunderstanding here (I'm new to dotenv) but I was very very surprised that this .env
file is mandatory. I thougth, that's the point of dotenv:
- Use a
.env
file in your local environment - Use "real" env vars in production (no
.env
required there!)
I find it very weird, that there's an extra check required in my code, whether .env
exists. I think, the library should handle this transparently.
Or am I completely missing the point here?
from phpdotenv.
@mikehaertl this is exactly the problem I was just trying to wrap my head around.
(Also completely new to dotenv)
And this is still actual, so I assume it's okay to comment in this old issue.
The way I solved it now was to just reverse the statement and check if APP_ENV is not in production.
i.e:
/*
* PHP DotEnv initialization
*
* Production expects to have everything in the .env exported,
* as actual environment variables on the server(s)
*/
if(getenv('APP_ENV') !== 'production') {
$dotEnv = new Dotenv(dirname(__DIR__));
$dotEnv->load();
}
Not quite sure if this is the recommended way of using it @vlucas ?
Sorry if I'm asking something obvious, it just really is unclear to me right now and it seems also to others, at least judging by what I read over the last 2 hours Googling this :D.
PS: Doesn't that also mean that dotenv should be installed with the dev flag for composer?!
composer require --dev vlucas/phpdotenv
from phpdotenv.
@sagikazarmark that makes perfect sense. I do exactly that. In fact, this is real code I just copied from a live application of mine running on bullet:
define('BULLET_ENV', $request->env('BULLET_ENV', 'development'));
// ...
// Load required environment variables from .env in development
$dotenv = new Dotenv\Dotenv();
if(BULLET_ENV == 'development') {
$dotenv->load(dirname(__DIR__), 'dev.env');
}
// Required ENV vars for this app in production
$dotenv->required([
'DATABASE_URL',
'PATH_FILE_UPLOADS',
'GOOGLE_PLACES_API_KEY',
'RAVEN_DSN',
'MANDRILL_USER', 'MANDRILL_PASS'
]);
from phpdotenv.
Also, related to the CLI context: wouldn't it be better to use getcwd()
instead of __DIR__
?
from phpdotenv.
Step 2 in your new flow with dotenv can be changed to running a single command:
source .env
from phpdotenv.
Of course it can be, that was just an example of setting up variables in our environment. Thanks anyway. :)
from phpdotenv.
@vlucas Anything to add here?
from phpdotenv.
I think Dotenv is always going to expect the file to exist. You could either try/catch, or just check it yourself:
if (file_exists($envDir . '.env')) {
$dotenv->load($envDir);
}
from phpdotenv.
Of course, the other solution might be something like:
$dotenv->loadIfExists();
from phpdotenv.
That's what I was hoping ;)
from phpdotenv.
$dotenv->loadIfExists();
and then
$dotenv->overloadIfExists();
feels like overkill.
I'd rather check the file exists or wrap it up in a try … catch as Vance said.
from phpdotenv.
You generally should only be using Dotenv for development and/or testing, e.g.:
if(getenv('APP_ENV') === 'development') {
$dotenv = new Dotenv\Dotenv();
$dotenv->load(__DIR__);
}
This is why the .env
is generally always expected to be present.
from phpdotenv.
Despite Vance being quite resolute in saying that this should not be used in production, that is a subjective viewpoint that has no bearing on how people actually use dotenv.
I think you have 2 camps:
- People who use it in development to simulate production environment variables
- People who use it in all environments to put their app in a known state so they don't have to rely on external environment variables.
The first camp, right now, has to check for the file as it's optional. However if you were to make the suggested change you would just be forcing the second camp to do the check to ensure the file is present.
Such a pointless change and one you could argue over until the cows come home.
from phpdotenv.
I agree with @vlucas: using dotenv in production SHOULD be avoided if POSSIBLE. The extra file loading is overhead. That said, it is just not possible in some cases.
However it doesn't really matter for example in case of a console application.
from phpdotenv.
@vlucas related to the recommended usage, I just realised something: Dotenv is not just able to load AND validate env variables, it can do valdation on it's own.
So here is how I would modify the "official" recommendation (as an option, since validation is not necessary):
$dotenv = new Dotenv\Dotenv();
if(getenv('APP_ENV') === 'development') {
$dotenv->load(__DIR__);
}
$dotenv->required('OTHER_VAR');
This allows us to skip file loading, but not the validation.
Does that make sense?
from phpdotenv.
I've created a PHP extension to handle .env
files in production. Its still very beta, and could use some testers, so if anyone wants to give it a go: https://github.com/koenpunt/php-dotenv
from phpdotenv.
I think this has been sufficiently answered, so I am closing this for now.
from phpdotenv.
Hi there,
On master branch (and all latest branches & 2.0.1), the Dotenv constructor should take directory and file name.
Using a conditional to use load or not seems weird.
$dotenv = new Dotenv\Dotenv(dirname(__DIR__) . '/etc', 'config.env');
if(false === $isProductionEnvironment) {
$dotenv->load();
}
$dotenv->required(['FOO']);
Should constructor be updated to avoid parameters ?
Regards
from phpdotenv.
Related Issues (20)
- How to loads many env file, but the extension is .txt HOT 1
- Fatal error: Uncaught Error: Class "Dotenv\Dotenv" not found in HOT 1
- Performance degradation HOT 3
- Question - are there any 'builtin' variables? HOT 5
- Add reader with support for secret files HOT 1
- loading a .env file from outside the webspace seem to not work
- php 8.2 function getenv() not set HOT 1
- Please dont use $_SERVER HOT 1
- Load AWS system environments HOT 4
- symfony/dotenv Correlation HOT 1
- PHP version 7.4 HOT 1
- How does this help with php_value in .htaccess?
- Is there anyway to use sections created in the .env file? HOT 4
- Fatal error: Uncaught Error: Class "Dotenv\Dotenv" not found in HOT 2
- Can one use Heredoc syntax for multi-line strings? HOT 1
- Failed to parse dotenv file due to unexpected whitespace HOT 1
- Inconsistent result when calling `$this->dotenv->load()` multiple times from PHPUnit HOT 1
- Simple script doesn't work. HOT 3
- How should this library be used when multiple dependencies of a project require it?
- Nesting Variables do not work for me on a raspberry
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from phpdotenv.