Laravel Configuration - How to Create and Using Configuration

Laravel Configuration

Best PHP Training with Live Project in Noida Fee is only 7500/-Click here for FREE DEMO.

/ins>

Introduction to Laravel Configuration

Config directory contains all of the configuration files for the Laravel framework. All option is documented, so feel free to glance within the data or file and get intimate with the advantages available to you.

In the preceding chapter, we have seen and discussed that the essential configuration files of Laravel are involved in the config directory. In this chapter, let us consider the categories comprised in the configuration.

 

Environment Configuration

laravel environment configuration file is necessarily desirable for the working context, and it is often advantageous to have unique configuration values based on the environment where the application is running. For instance, you may crave to apply a different cache driver provincially than you do on your production server.

Environment variables are those which render a directory of web services to your web application. All the environment variables are indicated in the .env file which comprises the parameters needed for initializing the configuration.

 

To address this a shoo-in, Laravel appropriates the DotEnv PHP library by Vance Lucas. In a current laravel configuration files installation, the root directory of your application will include a .env.example file. If you install Laravel through Composer, this file will be renamed as soon as it gets installed to .env. Else you should rename the file manually.

 

Your .env file should not be allocated to your application’s source controller considering each developer/server managing your application could need a diverse environment configuration. Moreover, this would be a redemption hazard in the event an invader obtains entrance to your source control receptacle since any delicate credentials would get revealed.

 

If you are generating amidst a team, you may envy to proceed including a .env.example file with your application. Through putting place-holder values in the example configuration file, additional or other representative developers regarding your team can undoubtedly understand which environment variables are required to run your application. The variables are needed to run the desired application, you may also produce an .env.testing file, that will revoke the .env file when operating PHPUnit tests or performing Artisan commands with the –env=testing option.

 

Environment Variable Type

In laravel database configuration .env files allow every variable to pass through it as a string. Therefore some reserved values have been generated to facilitate you to return a broader range of types from the env() function:

 

If you require to define an environment variable including a value which incorporates spaces, you may do so by embedding the value in double quotes. A below-mentioned example will clear your doubts.

 

APP_NAME=”My Application”

 

Retrieving Environment Configuration

The entire variables that are placed or listed in this file will be loaded inside the $_ENV PHP super-global when your application acquires a request. Though, you may utilize the env helper to recover or retrieve values from these variables in your configuration files. If you interpret the laravel database configuration, you will mark numerous of the options already utilizing this helper:

 

‘debug’ => env(‘APP_DEBUG’, false),

 

The second value as you can see is a value passed to the “env” function is the “default value.” which will be utilized if no environment variable endures for the addressed key.

 

Determining the Current Environment

APP_ENV variable from your .env file determines the current application environment via the APP_ENV variable from your .env file. You may locate this value through the environment method on the App facade:

 

$environment = App::environment();

You can pass arguments to the environment system to verify if the environment meets a furnished value. The said method will respond or return true if the environment meets any of the given values:

 

if (App::environment(‘local’)) {

   // The environment is local

}

 

if (App::environment([‘local’, ‘staging’])) {

   // The environment is either local OR staging…

}

 

Hiding Environment Variables from Debug Pages

Debugging requires when you try to run the program, to list unwanted error and to resolve it. When an exception is unable to get caught, and the APP_DEBUG environment variable is correct(true), the debug page will display all working environment variables and their contents. It may be possible in some cases; you may need to hide specific variables. You are able this by modernizing the debug_blacklist option in your config/app.php laravel configuration files.

 

Some of the variables are accessible in both the environment variables and the server/request data. Hence, you may want to blacklist them for both $_ENV and $_SERVER:

 

 

 

Accessing Configuration Values

Accessing configuration values is quite simple, and you can effortlessly locate your configuration values applying the global config helper function from everywhere in your application. The “dot” syntax can be used to obtain the configuration values, which comprises the name of the file also option you crave to access. A default value may also be defined and will be returned if the configuration option does not exist:

 

$value = config(‘app.timezone’);

To initiate configuration values at runtime, pass an array to the config helper:

 

config([‘app.timezone’ => ‘America/San Francisco’]);

 

configuration Caching

Laravel config cache clear is the most necessary function in configuration is caching which is used to provide your application a speed boost; you need to cache each configuration files of yours into an individual or a single file utilizing the config: cache Artisan command. This, in turn, will consolidate all of the configuration possibilities for your application inside a separate folder or file which will be stored and loaded quickly by the framework.

 

You need to run the PHP artisan config: cache command as part of your creation deployment system. The preceding command should not run while local development as configuration options will invariably require to be modified during your application’s development.

 

Maintenance Mode

The application requires maintenance thus when your application is in preservation or maintenance mode; your application will allow a custom view to display for all requests in the application itself, makes simple to “disable” application while it is refreshing or updating or when you are operating maintenance mode which is. A laravel maintenance mode analysis is involved in the default middleware stack for the application you are developing. If your application is in maintenance mode, a MaintenanceModeException will be launched including a status code of 503.

 

To facilitate maintenance mode, perform the down Artisan command:

 

Php artisan down

You are also allowed to provide information or message and retry options to the down command. The message value can be used to perform or log a custom message, while the retry value needs to be placed as the Retry-After HTTP header’s value:

 

For instance: php artisan down –message=”Upgrading Database” –retry=60

Despite while in maintenance mode, particular IP addresses or networks may be authorized to access the application utilizing the commands allow option:

 

For example: php artisan down –allow=127.0.0.1 –allow=192.168.0.0/16

To impair or disable maintenance mode, you need to use the up command:

 

Php artisan up

 

Maintenance Mode & Queues

The maintenance mode disagrees to handle any queued jobs while the application you are working on is in maintenance mode. As soon as the application is out of maintenance mode, the jobs will continue to be handled as normal mode.

 

Alternatives To Maintenance Mode

your application needs to have several seconds of downtime granted by maintenance mode require, consider alternatives like Envoyer to achieve zero-downtime deployment with Laravel maintenance mode.

Keep Updating yourself