How to do Laravel Configuration

Last updated on Jul 20, 2021

Read 446 times

The config directory contains the fundamental Laravel configuration files.  Let's look at the categories that make up the setup in this article.

Configuration Of The Environment

Environment variables offer you a list of web services to your web application. The .env file declares all of the environment variables and the parameters needed to start the configuration.

The .env file includes the following arguments by default:

APP_ENV = local
APP_DEBUG = true
APP_KEY = base64:ZPt2wmKE/X4eEhrzJU6XX4R93rChghghgwYG8E2f8QUA7kGK8 =
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 Considerations

The following considerations should be kept in mind while dealing with Laravel's essential configuration files: 

  1. The .env file should not be submitted to the application source control since each developer or user has their predetermined environment setting for the web application.
  2. The development team should provide the .env.example file for backup options containing the default configuration.

Environment Variables Retrieval

The env-helper functions may access all of the environment variables defined in the .env file by calling the appropriate parameter. When the program gets a request from the user, these variables are also displayed in the $_ENV global variable. The environment variable can access as illustrated below.

'env' => env('APP_DEBUG ', 'true'),

 

In the config folder's app.php file, the env-helper functions are called. The fundamental local parameter is required in the example above.

Getting to the Values of Configuration

The global config helper function allows you to retrieve configuration values from anywhere in the application quickly. Returned Default values are if the configuration settings are not populated.

To see the following code to set the default time zone:

config(['app.timezone' => 'Europe/Paris']);

Configuration Caching

It is critical to cache all configuration data to improve speed and enhance the web application. The command to cache configuration settings is.

config:cache

The screenshot below depicts caching systematically.

E:\xampp\htdocs>cd active

E:\xampp\htdocs\active>php artisan config:cache
Configuration cache cleared!
Configuration cached successfully!

Mode Of Operation

On occasion, you may need to make changes to your website's setup or perform maintenance. Keeping it in maintenance mode makes things easier for you in these situations. MaintenanceModeException with a status code of 503 is for web applications supported in maintenance mode.

May enable the maintenance mode on your Laravel web application with the following command.

E:\xampp\htdocs\active>php artisan down
Application is now in maintenance mode.

Then it shows that application or website is under maintaince or any other message of under construction. 

After you've finished working on updates and other maintenance, use the following command to turn off the maintenance mode on your web application.

E:\xampp\htdocs\active>php artisan up
Application is now live.

The website displays the output in an excellent working state, indicating that the maintenance mode is eliminated, and the website will be live again. 

Share on

Tags

1 Comments

  • comment img

    Muhammad Umer

    Aug 05, 2021

    Thanks this article help me to solve my issue of how to create custom helper in laravel.

    Reply

Leave a comment

Subscribe to see what we're thinking

Subscribe to get access to premium content or contact us if you have any questions.

Subscribe Now