Closed bjuppa closed 7 years ago
composer require vlucas/phpdotenv
Create .env.example
in project root directory
Add .env
in .gitignore
Add this post-root-package-install
script in composer.json
:
{
"scripts": {
"post-root-package-install": [
"php -r \"file_exists('.env') || copy('.env.example', '.env');\""
]
}
}
Then you can require_once
PATH/TO/vendor/autoload.php
from any php-file (for example in wp-config.php
) and then load the .env
file, e.g.:
(new Dotenv\Dotenv(__DIR__.'/..'))->load();
...and then use getenv()
to retrieve values!
This gist contains a function definition for an env()
method that takes default values: https://gist.github.com/bjuppa/50d26bb9ad6b4d6f66916be4158a9189
I'm planning to create a package fewagency/env
that would require vlucas/phpdotenv
and expose an init-method that loads the .env
using Dotenv::load()
and load the env()
helper method from the gist above.
Here's the package: https://github.com/fewagency/env
All done!
For Craft: https://mattstauffer.co/blog/environment-specific-configuration-for-craftcms-using-phpdotenv (uses
getenv()
)