Laravel env()
A Comprehensive Guide to Laravel .env
Configuration
Laravel, a popular PHP framework, provides developers with a powerful and convenient way to manage configuration settings for their applications using the .env
file. This file plays a crucial role in Laravel development, enabling you to customize various aspects of your application based on the environment it is running in.
In this article, we’ll explore how to effectively use the Laravel .env
file, the benefits it offers, and important considerations to watch out for while working with it.
Usage
Laravel ships in with a file called: .env.example
. All you have to do is rename it from .env.example
to .env
and you are ready to use the env configuration.
Now you can call it everywhere inside your application using env(VARIABLE_NAME);
.
Defining variables
Inside the .env
file, you can define variables like database credentials, app URL, debug mode, and more, using the format KEY=VALUE
. For example:
VARIABLE_NAME=VariableValue
APP_NAME=LaravelApp
APP_DOMAIN=example.com
APP_URL="http://${APP_DOMAIN}"
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=my_database
DB_USERNAME=my_username
DB_PASSWORD=my_password
NOTE: If you have space in variable values make sure to wrap them in quotes. like so:
APP_NAME="Cool laravel Applicaiton"
One thing I tend to do is use variables inside the env file whenever possible. You can access a variable in the env file using this syntax: ${VARIABLE_NAME}
, for example:
APP_DOMAIN=example.com
APP_URL="http://${APP_DOMAIN}" # Accessing the APP_DOMAIN variable
SESSION_DOMAIN=".${APP_DOMAIN}"
SANCTUM_STATEFUL_DOMAINS=".${APP_DOMAIN}"
This is very helpful when you use the same settings in different variable names, and sometimes happens that you forget to change the domain name in all the places. Using this approach, all you need to do is change it only in one place.
Accessing Configuration Variables
In your Laravel code, you can access these configurations using the env()
function. For example, to access the app domain:
$appName = env('APP_DOMAIN');
This allows for easy and clean retrieval of configuration variables throughout your application.
Things to Be Careful About
It is recommended that never use directly the env(‘APP_NAME’)
, but instead use configuration files. So for example instead of calling :
Route::get('/env', fn () => env('APP_DOMAIN'));
Do this:
Add the variable to config/app.php
file.
<?php
return [
/*
|--------------------------------------------------------------------------
| Domain Routing
|--------------------------------------------------------------------------
|
| You can use the admin panel on a separate subdomain.
|
| Example: 'example.com'
|
*/
'domain' => env('APP_DOMAIN'),
Route::get('/env', fn () => config('app.domain'));
The reason why is because laravel has a command called: php artisan config:cache
and after you have cached your config, any consecutive reading will read from the cache and ignore the env().
so if you call echo env(‘APP_DOMAIN’)
before the cache, will output example.com
, after you run php artisan config:cache
and calling again env(‘APP_DOMAIN’)
then the output will be null
. As it is stated in laravel documentation:
If you execute the
config:cache
command during your deployment process, you should be sure that you are only calling theenv
function from within your configuration files. Once the configuration has been cached, the.env
file will not be loaded and all calls to theenv
function for.env
variables will returnnull
.
Conclusion
Laravel’s .env
file simplifies the configuration management process for your application, offering flexibility and security. By understanding its structure and following best practices, you can effectively manage your Laravel project's configurations and ensure a secure and maintainable development environment.