Laravel 11 streamlined configuration files

Laravel 11 streamlined configuration files

 Laravel 11 streamlined configuration files


One of my favorite features in Laravel 11 is the streamlined configuration files. During the development of Laravel 11, all configuration files were removed from the default Laravel installation. However, a few weeks before release, Taylor decided to re-include a slimmed version of the config files in a default Laravel install but left the option to remove any files or options you don't need.

Let's dig a little deeper to understand how this works to avoid mistakes and get the slimmest application possible. After all, the config files can add a lot of noise to your application. You also need to be careful to keep them up-to-date, as they are constantly changing. This combination is why I'm glad to see this feature in Laravel 11.

Internally, Laravel merges your configuration file with a framework default. So, if your app has a config/database.php file, it will be merged with Laravel's internal config/database.php file.

What's interesting here is the merge. On the surface, this merges top-level options (a shallow merge). This means you can further slim your configuration files by removing any top-level options you do not use. Again, any options in your configuration file will automatically merge with the Laravel defaults.

Let's look at a quick example using the following config/app.php file within a Laravel 11 application:

<?php


return [

    'timezone'.         => 'Asia/Phnom Penh',

    'custom_option' => 'foo'

];

The resulting configuration would be all of the core app configuration options (app.nameapp.envapp.debug, etc) with app.timezone overwritten and your app.custom_option added.

This merge works well for files with top-level options. However, some of the configuration files have nested "driver" options.

Laravel does a bit more when performing this merge. Although it is not recursive, Laravel will merge some common nested options. For example, database.connectionsfilesystem.disksand more.

With this additional merge, instead of needing to include all the drivers under database.connections (since it's a top-level option), you may slim this section to only the drivers you use.

For example, if you use the default testing and mysql database drivers but also have a custom mysql_replica driver in Laravel 11, your config/database.php file may be:

<?php


return [

    'connections'=> [

        'mysql_replica' => [

            'driver'                => 'mysql',

            'url'                     => env('DB_REPLICA_URL'),

            'host'                  => env('DB_REPLICA_HOST', '127.0.0.1'),

            'port'                  => env('DB_REPLICA_PORT', '3306'),

            'database'          => env('DB_DATABASE', 'laravel'),

            'username'         => env('DB_REPLICA_USERNAME', 'root'),

            'password'         => env('DB_REPLICA_PASSWORD', ''),

            'unix_socket'     => env('DB_SOCKET', ''),

            'charset'            => env('DB_CHARSET', 'utf8mb4'),

            'collation'          => env('DB_COLLATION', 'utf8mb4_0900_ai_ci'),

            'prefix'               => '',

            'prefix_indexes' => true,

            'strict'                => true,

            'engine'             => null,

            'options'            => extension_loaded('pdo_mysql') ? array_filter([

                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),

            ]) : [],

       ],

    ]

];

Of course, you are welcome to preserve the entire set of default configuration files with all of their options. But if you like Laravel’s new, slimmer application structure and want to reduce the noise in your configuration files to your true customizations, this is the way to go.

Reactions

Post a Comment

0 Comments

close