Working on Laravel Applications often requires accessing important variables from the configuration file, namely called the .env file. These are the .env variables, holding important data for the application. We can use the Laravel env()
helper to access these variables. However, there is a more safer way to access these variables and also create new .env variables to add in .env files using the config()
helper.
You can read variables from the .env file using the config() helper function. For example, to read the app name, you can use config('app.name')
. We can read these variables using the env()
helper function also. But, it is not recommended to use it to get data as it returns empty variables if the configuration is cached with the command shown below.
php artisan config:cache
This command generates a file in bootstrap/cache/config.php that contains all the config values in an optimized format for better speed.
php artisan config:clear
The command above will clear the cache again by removing even the bootstrap/cache/config.ph
p file.
To access configuration variables we can use the config helper or even the config facade, as shown below.
// The most common and recommended way
$appName = config('app.name');
// An equivalent way:
use Illuminate\Support\Facades\Config;
$appName = Config::get('app.name');
// Avoid using env file:
$appName = env('APP_NAME');
Add Custom Config Variables to the .env file
Let’s go through the steps to add your own custom variables to an .env file. This variable can be accessed throughout your Laravel application.
Step 1: Define your Custom Variables in .env file
SAMPLE_VARIABLE=value
Next, create a new file inside the config folder as custom.php, and define an array inside it that returns the value of your environment variable.
return [
'sample_variable' => env('sample_variable'),
];
Now, you can access this variable using the config() helper function.
$value = config(sample.your_variable');
This value can be accessed anywhere in your application.
<a href="{{ config(sample.your_variable') }}">Link</a>
Laravel default config files
The config folder in Laravel holds many files responsible for the application’s operations. A newly installed Laravel project will provide the following config files.
File | Location | Aim |
app.php | /config/app.php | Application level settings |
auth.php | /config/auth.php | Authentication settings |
broadcast.php | /config/broadcast.php | Real-time event broadcasting data |
cache.php | /config/cache.php | Cache configuration |
cors.php | /config/cors.php | Cross origin resource sharing data |
database.php | /config/database.php | Database settings and data |
filesystems.php | /config/filesystems.php | Filesystems configuration |
hashing.php | /config/hashing.php | Hashing data for password hashing |
logging.php | /config/logging.php | Logging configuration |
mail.php | /config/mail.php | Email configuration |
queue.php | /config/queue.php | Queue configuration |
sanctum.php | /config/sanctum.php | Laravel Sanctum configuration |
services.php | /config/services.php | External services configuration |
session.php | /config/session.php | Session configuration |
view.php | /config/view.php | View configuration |
Config files are better to use as compared to .env files for a number of reasons.
- Organized Data: they provide a structured approach to manage data, group related settings together. So, they are easier to locate and update.
- Better Version Control: config files can be easily tracked and managed using version control systems like Git.
- Flexible: Complex configuration structures, arrays, and nested loops can be defined in config files. Thereby, making them more flexible than .env files.
- Caching: Laravel caches configuration files, and significantly improves application performance. However, caching is not possible with .env files, and accessing each environment variable requires file parsing, which leads to application overhead.
- Readable Code:
config()
helper function allows better code readability as compared to directly accessing the .env file.
Reading data from configuration files is the recommended approach to access .env variables. It ensures we get data as per the predictable app behavior and avoid issues with empty values and configurations. Moreover they provide numerous benefits like organized data structure, flexible application structure, better application caching, flexible, and readable code.