Laravel 7 Application Configuration Tutorial




Laravel Application Configuration Tutorial. In this tutorial, you will learn how to configure the laravel applications.

In the previous tutorial, you have known about the laravel config directory. This directory holds all the configuration files of our web applications. The config directory holds sub config directories such as database, mailing, services, auth, cache and session, etc.

The configuration of the Laravel web application allows you to define the configuration per environment. Here, you will learn about various configurations in laravel web applications and how you can do basic configuration in these applications.

Environment Configuration

You can configure the Environment variables in .evn file. You find this file from your laravel application root directory. In this file you can set / configure database details, app mode detail like production / test, set mail smtp details etc.



Access Environment Variables

If you want to access environment variables from anywhere in your laravel web application. You can access or get like below:

'env' => env('APP_NAME', 'laravel'),

Database Configuration

In this file, You can also configure or set multiple database connections. So go to config/database.php set database credentials like this below:

    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', ''),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null,

Configuration Caching

Whatever be the web application, everyone wants this. The application loads fast and the application does not take much time to load. So to speed up your larval web application, you should cache all your configuration files into one file using the configuration using the cache artisan command. This will merge all the configuration options of your larva web application into a special file that will be loaded quickly by the framework.

You should usually run the php artisan config:cache command as part of your production deployment routine.

Maintenance Mode

Whenever you need to put your Laravel web application in maintenance mode. So you can use the below command to handle the application in down and up mode.

To enable the laravel application maintenance mode by the following command −

php artisan down

To disable the laravel application maintenance mode by the following command −

php artisan up