Laravel (5.7) Configuration
Introduction
All of the files responsible for configuration in Laravel are all stored in the config directory. A screenshot is seen below.
The different files listed are responsible for various configurations.
Environment Configuration
Based on the environment where the laravel application is running, the configuration values in these environments would be different. For instance, you would want to use a different configuration in local development than when the application is in production. Laravel utilizes theDotEnv PHP library which comes with a .env.example file. This is the file where configuration for the application is specified.
When using a source control platform such as Github,you should be careful not to commit your .env file because another developer using your application may require a different configuration based on the environment. Furthermore, this would be a security risk in the event an intruder gains access to your source control repository, since any sensitive credentials would get exposed.
Environment Variable Types
All variables in your .env files are parsed as strings, so some reserved values have been created to allow you to return a wider range of types from the env() function.
.env Value | env() Value |
true | (bool)true |
(true) | (bool)true |
false | (bool)false |
(false) | (bool)false |
empty | (string)" |
(empty) | (string)" |
null | (null)null |
(null) | (null)null |
If you need to define an environment variable with a value that contains spaces, you may do so by enclosing the value in double quotes.
APP_NAME="My Application"
Retrieving Environment Configuration
all the variables listed in this file will be loaded into the $_ENV PHP superglobal when your application receives a request. Yet, you may use the env helper to retrieve values from these variables in your configuration files
'debug' => env('APP_DEBUG', false),
Determining The Current Environment
the current application environment is determined by the APP_ENV variable from your .env file.
This value can be accessed through the environment method on the app facade.
$environment = App::environment();
You may also pass arguments to the environment method to check if the environment matches a given value.
if (App::environment('local'))
{
// The environment is local.
}
if (App::environment('local', 'staging'))
{
// The environment is either local OR staging.
}
Accessing Configuration Values
The configuration values may be accessed using "dot" syntax, which includes the name of the file and option you wish to access. A default value may also be specified and will be returned if the configuration option does not exist.
$value = config('app.timezone');
To set configuration value at runtime, pass an array to the config helper.
config(['app.timezone' => 'America/Chicago']);
Configuration Caching
To give your application a speed boost, you should cache all of your configuration files into a single file using the config:cache Artisan command. This will combine all of the configuration options for your application into a single file which will be loaded quickly by the framework.
You should typically run the php artisan config:cache command as part of your production deployment routine.
Maintenance Mode
When your application is in maintenance mode, a custom view will be displayed for all requests into your application. This makes it easy to "disable" your application while it is updating or when you are performing maintenance. A maintenance mode check is included in the default middleware stack for your application. If the application is in maintenance mode, a MaintenanceModeException will be thrown with a status code of 503.
To enable maintenance mode, simply execute the down Artisan command:
php artisan down
You may also provide message and retry options to the down command.The message value may be used to display or log a custom message, while the retry value will be set as the Retry-After HTTP header's value:
php artisan down --message="Upgrading Database" --retry=60
Even while in maintenance mode, specific IP addresses or networks may be allowed to access the application using the command's allow option:
php artisan down --allow=127.0.0.1 --allow=192.168.0.0/16
To disable maintenance mode, use the up command.
php artisan up
Maintenance Mode Response Template
The default template for maintenance mode responses is located in resources/views/errors/503.blade.php.
You are free to modify this view as needed for your application.
Maintenance Mode & Queues
While your application is in maintenance mode, no queued jobs will be handled. The jobs will continue to be handled as normal once the application is out of maintenance mode.
Alternatives To Maintenance Mode
Since maintenance mode requires your application to have several seconds of downtime, consider alternatives like Envoyer to accomplish zero downtime deployment with Laravel.
Previous:
Laravel (5.7) Tutorial
Next:
Laravel (5.7) Directory Structure
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/laravel/configuration.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics