Comments (5)
The primary advantage with the current approach is that it makes the variables more easily exportable to shell variables.
export APP_ENV='staging'
export DATABASE_URL='sqlite::memory::'
Allows you to do this in the local shell:
eval $(cat .env)
Of course if you use comments and other things, then it might not be so straightforward.
from phpdotenv.
That said, I definitely feel weird about a bunch of special parsing rules, and I do think this project would benefit from the option of a PHP file include as well.
from phpdotenv.
Ah, didn't even know I could do that - it isn't mentioned anywhere in the docs, and I just dug through the source code right now to verify it. I personally don't see a use case for that - but to be complete, if your file was:
<?php
return [
'APP_ENV' => 'staging',
'DATABASE_URL' => 'sqlite::memory::'
];
you could do something like:
eval $(php -r 'foreach (require ".env" as $key => $value) { echo "export {$key}={$value}\n"; }')
Now, I wouldn't ever do this, and it is a bit ugly, and far more complicated than eval $(cat .env)
, but it's just a proof of concept that a one-liner is possible here as well, if you need it for some deployment process or something - additionally, it doesn't even matter if you have comments or whatever, since PHP already knows how to parse PHP files, so we don't need to write our own parser as we do for the current syntax.
So anyway, you'd be willing to accept a PR that allows PHP files to be used as well? This wouldn't be too complicated actually - we could refactor these lines into a few methods with some detection logic for whether we have a PHP file or not (for example by checking if it starts with <?php
, since a valid .env
file, according to the current syntax, can't start with that - or just checking if the passed configuration filename ends with .php
), and using the appropriate "loading" method afterwards - and this would be completely backwards compatible as well.
Thanks for your input!
P.S. I now realize my mistake when creating this issue and writing about different things, and I'll make sure not to do that again - but what do you think about the apache_setenv()
addition - would that make sense? I'm happy to make both PRs, if you think they'd be welcome.
from phpdotenv.
Running source .env
should load the environment variables into the shell as well.
If you really want to use PHP, then you can avoid using load()
and instead call Dotenv::setEnvironmentVariable()
in a PHP file.
Re: the apache bug – I never realised that existed… interesting. I think it'd be difficult to get around it with a solution that doesn't then become confusing. Could it have weird knock-on consequences for Apache?
Definitely think it's worth documenting though, that could catch some people out.
Also, you can use Dotenv::findEnvironmentVariable()
to get an environment variable, and it checks the $_ENV
first. If that's too verbose, you could create your own wrapper function, e.g. function findenv()
from phpdotenv.
Forgive me for reviving a long-dormant issue, but the original issue raised by @levacic regarding Apache env variables is unresolved. Considering the docs claim that Dotenv::overload overrides existing environment variables, I had thought this issue had been solved. The problem, however, is reproducible just as @levacic noted:
SetEnv FOO bar
in Apache configuration- Set
FOO not_bar
in my project's .env - Use
$dotenv->overload();
echo getenv(FOO)
=> 'bar' (I expect--and want--'not_bar'!)
The solution, as noted, is to check if the apache_setenv
function exists (and, if you want, check if apache_getenv
returns truthy), then use apache_setenv
.
Is there a reason why you wouldn't want to support this feature?
from phpdotenv.
Related Issues (20)
- Unable to read the "/tmp/build_2f06b5cf/.env" environment file HOT 1
- auto_detect_line_endings is deprecated (PHP 8.1 + dotenv v2.6.9) HOT 1
- PHP version 7.1 is not supported now HOT 2
- Equals Symbol in last line of multiline variable causes issues. HOT 3
- Save to .env HOT 1
- 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 3
- Add reader with support for secret files
- 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
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.