Laravel - Configuration

In the previous chapter, we saw that the main Laravel configuration files are included in the config directory . In this chapter, let's discuss the categories included in the configuration.
Environment configuration
Environment variables are those that provide a list of web services to your web application. All environment variables are declared in the .env file, which includes the settings needed to initialize the configuration.
By default , the .env file contains the following options:
APP_ENV=local APP_DEBUG=true APP_KEY = base64:ZPt2wmKE/X4eEhrzJU6XX4R93rCwYG8E2f8QUA7kGK8 = APP_URL = http://localhost DB_CONNECTION = mysql DB_HOST = 127.0.0.1 DB_PORT = 3306 DB_DATABASE = homestead DB_USERNAME = homestead DB_PASSWORD = secret CACHE_DRIVER = file SESSION_DRIVER = file QUEUE_DRIVER = sync REDIS_HOST = 127.0.0.1 REDIS_PASSWORD = null REDIS_PORT = 6379 MAIL_DRIVER=smtp MAIL_HOST=mailtrap.ioMAIL_PORT=2525 MAIL_USERNAME = null MAIL_PASSWORD = null MAIL_ENCRYPTION = null
Important Points
When working with the main Laravel configuration files, the following points should be noted:
-
The .env file should not be committed to the application's source control system because each developer or user has a predefined environment configuration for the web application.
-
For backup options, the development team must include a .env.example file , which must contain the default configuration.
The .env file should not be committed to the application's source control system because each developer or user has a predefined environment configuration for the web application.
For backup options, the development team must include a .env.example file , which must contain the default configuration.
Retrieving Environment Variables
All environment variables declared in the .env file can be accessed through the env-helper functions that will call the appropriate option. These variables are also specified in the $_ENV global variable whenever the application receives a request from the user. You can access the environment variable as shown below −
'env' => env('APP_ENV', 'production'),
The env helper functions are called in the app.php file , which is located in the config folder . The above example calls the base local parameter.
Accessing Configuration Values
You can easily access configuration values ​​anywhere in the application using the global helper function config. If the configuration values ​​are not initialized, the default values ​​are returned.
For example, to set the default timezone, the following code is used −
config(['app.timezone' => 'Asia/Kolkata']);
Configuration caching
To improve performance and improve the efficiency of a web application, it is important to cache all configuration values. The command to cache configuration values ​​is −
config:cache
The following screenshot shows caching in a systematic approach −
Maintenance mode
Sometimes you may need to update some configuration settings or perform maintenance on your website. In such cases, keeping it in maintenance mode will make it easier for you. Such web applications that are in maintenance mode will throw an exception, namely MaintenanceModeException with status code 503.
You can enable maintenance mode in your Laravel web application using the following command:
php artisan down
The following screenshot shows what the web application looks like when it is not running.
Once you are done with updates and other maintenance, you can turn off maintenance mode in your web application using the following command −
php artisan up
Now you may find that the website is showing a result with proper functioning and showing that maintenance mode has now been removed as shown below −