Jose Jimenez
Jose Jimenez
Software Architect & Developer
> >

Leveraging Laravel configuration and environment variables in your application

Published in Laravel, PHP on Jul 29, 2022

Learn how to leverage configuration and environment variables for improved readability and performance in your applications.

A common mistake I am seeing with many developers is the constant usage of env() to pull configuration settings within their application. The reason to avoid this is due to performance, once the application is in production config:cache will be executed and env() will no longer be able to get environment variables.

An additional benefit to moving everything to config files is that it will make your application cleaner in the long run.

Setting up the .env

You should store your configuration files in an .env depending on your use cases.

Quick tips

  1. Add any sensitive keys in your .env and make sure to add ignore the file in your respective repository. ie !gitignore in git. for security reasons.
  2. Prefix and group your settings to quickly reference them at a later point, add a brief note about what it is to make it easier for yourself.
  3. You can can add and comment out specific settings for later usage SETTINGS_SORT_PROFILES and rely on a default to comment out and use at a later stage.(more on this later)

.env

1# Default Application Settings
2#SETTINGS_SORT_PROFILES=ASC
3SETTINGS_PROFILE_MODE=all
4
5# Merchant Information
6MERCHANT_ENABLE_TESTING_ENV=true
7MERCHANT_POSTBACK_URL=https://somedomain.com/postback.php

Creating the config/ files

For this example we will be creating 2 files, a settings.php and a merchant.php configuration file. I use the settings.php as a generic catch all for my configuration file, and try to create additional files to better organize larger applications.

config/settings.php

1return [
2 'profiles_per_page' => env('SETTINGS_PROFILES_PER_PAGE', 3),
3 'sort_profiles' => env('sort_profiles', 'DESC'),
 4 
5 /*
6 |--------------------------------------------------------------------------
7 | The mode utilized to get profiles
8 |--------------------------------------------------------------------------
9 |
10 | ALL: Get all profiles
11 | RANDOM: Get random profiles
12 | CUSTOM: Get profiles based on custom criteria
13 |
14 */
15 'profile_mode' => env('SETTINGS_PROFILE_MODE', 'ALL'),
16];

config/merchant.php

1return [
2 /*
3 |--------------------------------------------------------------------------
4 | Enable testing environmenbt
5 |--------------------------------------------------------------------------
6 |
7 | If set to true application will run in testing mode, allowing for test sales to be made.
8 |
9 */
10 'enable_testing_env' => env('MERcHANT_ENABLE_TESTING_ENV', false),
11 
12 /*
13 |--------------------------------------------------------------------------
14 | Postback URL
15 |--------------------------------------------------------------------------
16 |
17 | The URL that will be used as a postback from the merchant when a transaction is made.
18 |
19 */
20 'postback_url' => env('MERCHANT_POSTBACK_URL', 'https://domain.com/postback'),
21];

Things to notice is how much cleaner and organized settings are, more information can be added about each setting, and possible value configurations to allow yourself or additional developers to review a setting in the future. Every setting can have a default value to ensure your application runs the way you intended it to.

I intentional commented out SETTINGS_SORT_PROFILES in .env to illustrate that it might be good to keep track of settings that you can quickly toggle during development without the need to add everything to your .env, which is why SETTINGS_PROFILES_PER_PAGE cannot be found on the .env listed above.

Using it inside your application

In your application you would do use the config() helper and reference the setting using dot notation. The first item in the list will be the file you are referencing, while the remaining keys allow you to reference any key from a nested array.

 1config('merchant.options.countries.enabled')
 2
3// would fetch
 4
5// config/merchant.php
 6
7return [
8 'allow_testing' => true,
9 'options' => [
10 'countries' => [
11 'enabled' => [
12 'us',
13 'uk',
14 ],
15 'disabled' => false,
16 ],
17 ],
18];
 1 
2class MainController
 3{
4 public function index()
5 {
6 $profiles = Profile::query()
7 ->where('is_open', true)
8 ->orderBy('name', config('settings.sort_profiles'))
9 ->paginate(config('settings.profiles_per_page'));
10 }
11}

Performance

Last but not least once you are ready to go to production don't forget to run php artisan config:cache which will cache all the settings into one file for perforance reasons. An important note here is that you will not be able to rely on any if statements during the configuration step, so think of the variables as static when working with them.