Laravel 11 streamlined configuration files

Laravel 11 streamlined configuration files

Laravel 11 Streamlined Configuration Files

One of the standout features of Laravel 11 is its streamlined configuration files. Initially, all configuration files were removed from the default Laravel installation. However, just weeks before the release, Taylor Otwell (Laravel's creator) decided to re-include a simplified set of configuration files. The best part? You now have the flexibility to remove any files or settings you don’t need.

This change is beneficial in several ways. It reduces unnecessary bloat in your application, and you can ensure that you only have the settings that are crucial for your project. But, you need to be mindful about keeping your configuration files up-to-date, as Laravel’s internal defaults change over time.

Let's take a closer look at how this works and how you can take advantage of it to create a leaner application.

Merging Configuration Files

Internally, Laravel merges your custom configuration files with the framework's default ones. So, for example, if your app has a config/database.php file, Laravel will merge it with the internal config/database.php file, which contains the default configuration settings.

The key thing here is the merge. On the surface, this is a shallow merge, which means it combines top-level settings. This allows you to further slim down your configuration by removing any top-level options that you don’t use. Any options in your configuration file will still be automatically merged with Laravel's internal defaults.

Example: Customizing config/app.php

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

<?php return [ 'timezone' => 'Asia/Phnom Penh', 'custom_option' => 'foo' ];

The resulting configuration would include all of the core app configuration options (e.g., app.name, app.env, app.debug, etc.) with:

  • app.timezone overwritten with Asia/Phnom Penh

  • app.custom_option added as foo

This merge is simple and works great for files with top-level options. However, some configuration files have nested options, such as the "driver" options.

Merging Nested Options

Laravel takes the merging process a step further for certain nested options, such as database.connections, filesystem.disks, and others. While Laravel doesn't perform a deep, recursive merge, it merges common nested options to help reduce redundancy.

For instance, if you're only using certain database drivers (e.g., the default mysql and testing drivers, along with a custom mysql_replica driver), you can slim down your config/database.php file as shown below:

<?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'), ]) : [], ], ], ];

In this case, only the mysql_replica driver configuration is included, which means your configuration is much leaner than including all default drivers.

Keeping or Removing Default Configurations

While you’re free to keep the full set of default configuration files with all their options, Laravel’s new structure provides a great opportunity for developers who prefer a more minimal setup. If you want to reduce noise in your configuration files and only include true customizations, this streamlined approach will help you achieve that.

Laravel 11's streamlined configuration files allow you to customize only what you need, reducing unnecessary bloat in your application. Whether you prefer keeping the full set of configuration files or slimmed-down versions, this feature gives you more control over your Laravel setup.

Souy Soeng

Souy Soeng

Hi there šŸ‘‹, I’m Soeng Souy (StarCode Kh)
-------------------------------------------
🌱 I’m currently creating a sample Laravel and React Vue Livewire
šŸ‘Æ I’m looking to collaborate on open-source PHP & JavaScript projects
šŸ’¬ Ask me about Laravel, MySQL, or Flutter
⚡ Fun fact: I love turning ☕️ into code!

Post a Comment

CAN FEEDBACK
close