Loads environment variables from .env to $_ENV and $_SERVER automagically, and optionally to getenv().
Special thanks to our sponsors
You should never store sensitive credentials in your code. Storing configuration in the environment is one of the tenets of a twelve-factor app. Anything that is likely to change between deployment environments – such as database credentials or credentials for 3rd party services – should be extracted from the code into environment variables.
Basically, a .env file is an easy way to load custom configuration variables
that your application needs without having to modify .htaccess files or
Apache/nginx virtual hosts. This means you won't have to edit any files outside
the project, and all the environment variables are always set no matter how you
run your project - Apache, Nginx, CLI, and even PHP's built-in webserver. It's
WAY easier than all the other ways you know of to set environment variables,
and you're going to love it!
- NO editing virtual hosts in Apache or Nginx
- NO adding
php_valueflags to .htaccess files - EASY portability and sharing of required ENV values
- COMPATIBLE with PHP's built-in web server and CLI runner
PHP dotenv is a PHP version of the original Ruby dotenv.
Installation is super-easy via Composer:
composer require vlucas/phpdotenvor add it by hand to your composer.json file.
We follow semantic versioning, which means breaking changes may occur between major releases. We have upgrading guides for V2 to V3, V3 to V4 and V4 to V5 available here.
The .env file is generally kept out of version control since it can contain
sensitive API keys and passwords. A separate .env.example file is created
with all the required environment variables defined except for the sensitive
ones, which are either user-supplied for their own development environments or
are communicated elsewhere to project collaborators. The project collaborators
then independently copy the .env.example file to a local .env and ensure
all the settings are correct for their local environment, filling in the secret
keys or providing their own values when necessary. In this usage, the .env
file should be added to the project's .gitignore file so that it will never
be committed by collaborators. This usage ensures that no sensitive passwords
or API keys will ever be in the version control history so there is less risk
of a security breach, and production values will never have to be shared with
all project collaborators.
Add your application configuration to a .env file in the root of your
project. Make sure the .env file is added to your .gitignore so it is not
checked-in the code
S3_BUCKET="dotenv"
SECRET_KEY="souper_seekret_key"Now create a file named .env.example and check this into the project. This
should have the ENV variables you need to have set, but the values should
either be blank or filled with dummy data. The idea is to let people know what
variables are required, but not give them the sensitive production values.
S3_BUCKET="devbucket"
SECRET_KEY="abc123"You can then load .env in your application with:
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();To suppress the exception that is thrown when there is no .env file, you can:
