Laravel Configuration and Environment Variables
Each laravel application has one “Config” directory which includes all the basic configuration files. You can configure your application by adding custom or built-in configuration variables.
Environment Configuration:
Based on the environment where the application is running, configuration values would be different. All the environment variables are stored in .env file.
By default, the .env file includes following parameters −
Note: Each developer/user has predefined environment configuration for his application, so this
file should not be committed to your application’s source control. If you want to keep default configuration, you can take backup of this file in the “.env.example” file.
Accessing Environment variables:
You can access the environment variable by using “env-helper” function. For example :
'debug' => env('APP_DEBUG', false)
Accessing Configuration variables: You can access the configuration variable anywhere in your application by calling config-helper function: For example:
$value = config('app.timezone');
You can also set the value of any configuration variable i.e.
config(['app.timezone' => 'America/Chicago']);
Configuration Caching:
If you want to increase the performance of your application, you can cache all the configuration values by invoking one command. This command will combine all of your configuration options into one file which will be loaded quickly every time you run your application.
The command for caching the configuration values is −
php artisan config:cache
Once you run this command, a new file will be created in app/bootstrap/cache/ folder. This file is named as “config.php”.
Maintenance Mode:
When you want to change the values of configuration variables or perform maintenance on your application, then you can keep your website in maintenance mode. A maintenance mode check is included in the default middleware stack for your application. This middleware will be executed on every request made to your application.
Maintenance mode of your application will be enabled by using following command:
php artisan down
After this, your website will throw an exception namely “MaintenanceModeException” with a status code of 503. Below will be the response when you will hit you application through browser-
You can also disable the maintenance mode of your application by executing the following command:
php artisan up
After this, your application will work perfectly as shown in below screenshot: