In this tutorial you will learn about the Laravel Configuration and its application with practical example.
The config directory contains all of your application’s configuration files.
Directory Permissions
After installing Laravel it is required to set the write permission for the storage and bootstrap/cache directory otherwise laravel will not work.
Application configuration
The application configuration information is located in /config/app.php. You can set or change various parameters for your application from here.
Debugging mode
Locate the following code
1 |
'debug' => env('APP_DEBUG', false), |
From here you can set debug mode.
Time zone
Locate the following code
1 |
'timezone' => 'UTC', |
From here you can set your preferred time zone, default is UTC
Application Key
Locate the following code
1 |
'key' => env('APP_KEY', 'some_random_string'), |
from here you can set encryption key for your application, it required to generate application key for security purpose.
Environmental Configuration
After installing Laravel; .env file is automatically created in root directory of your application. If it is not created; simply rename the .env.example file to .env file. This file contains various environment variables for your application.
Your .env file should resemble something similar to this.
1 2 3 4 5 6 7 8 9 10 11 12 |
APP_ENV=local APP_DEBUG=true APP_KEY=SomeRandomString DB_HOST=localhost DB_DATABASE=homestead DB_USERNAME=homestead DB_PASSWORD=secret CACHE_DRIVER=file SESSION_DRIVER=file QUEUE_DRIVER=sync |
Note: The first line in the above code snippet APP_ENV=local has been set. It can further be changed to production or testing as per your requirement.
Authentication configuration
The authentication configuration file is located in /config/auth.php, you can change it as per your application requirements.
Database configuration
The database configuration file is located in /config/database.php. In this file you can set different parameters for database, based on the database engine you are using. Below is configuration code for MySQL Database –
1 2 3 4 5 6 7 8 9 10 11 |
'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', '<host-name>'), 'database' => env('DB_DATABASE', '<database-name>'), 'username' => env('DB_USERNAME', '<user-name>'), 'password' => env('DB_PASSWORD', '<password>'), 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, ], |
Replace <host-name>, <database-name>, <user-name> and <password> with your hostname, database name, username and password correspondingly.
Naming the Application
The default namespace for laravel application is App; but if you want to change it to match your application name, which can be easily done using app:name Artisan command.
1 |
php artisan app:name <application-name> |
Replace <application-name> with the application name of your choice.
Maintenance Mode
Sometimes, you may need to put your application into maintenance mode for upgrades. This will make your application temporarily unavailable for public access. You don’t want errors to pop up when you are going through the important updates in the background, right?
Laravel made it easier for you, there are artisan commands available to start and stop the maintenance without any hassle.
Command to start Laravel Maintenance Mode
To put application into maintenance mode, execute the following command.
1 |
php artisan down |
Command to stop Laravel Maintenance Mode
Once you are done with upgrading application or making changes to it and want to make it live again, execute the following command.
1 |
php artisan up |
There is a default template for maintenance mode is located in resources/views/errors/503.blade.php. You are free to customize it as per your application requirement.