Documentation
Environment Variables
Loading environment variables from a .env file
On this page
Introduction#
It is often helpful to have different configuration values based on the environment in which your application is running, or to keep credentials out of your source control. To make this a cinch, Laravel Zero uses the DotEnv PHP library, which loads environment variables from a .env file at the root of your project.
Installation#
You may scaffold the required files using the app:install Artisan command:
php application app:install dotenv
The installer creates a .env file and a matching .env.example file at the root of your project, and adds .env to your .gitignore file.
Note: Your
.envfile should not be committed to source control. Each developer using your application may require a different environment configuration, and committing the file would be a security risk if it contains sensitive credentials. The.env.examplefile, on the other hand, should be committed, so your team can see which variables your application expects.
Retrieving Environment Variables#
Assuming your .env file contains the following:
TMDB_KEY=234567
You may retrieve the value using the env helper. The second argument passed to the env function is the "default value", which will be returned if no environment variable exists for the given key:
$key = env('TMDB_KEY'); $key = env('TMDB_KEY', 'default-key');
Note: As in Laravel, you should only call the
envhelper from within your configuration files. Elsewhere in your application, read the value through theconfighelper instead.
Environment Variables in a Build#
When your application has been compiled into a PHAR archive, Laravel Zero also looks for a .env file placed alongside the archive itself, and loads it on top of any variables that were already defined. This allows the people using your application to configure it without rebuilding anything:
. ├── .env └── movie-cli
Spotted a mistake? Edit this page on GitHub.