Laravel 11 streamlined configuration files

Published on by

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' => 'America/Kentucky/Louisville',
 
'custom_option' => 'foo'
 
];

The resulting configuration would be all of the core app configuration options (app.name, app.env, app.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.connections, filesystem.disks, and 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.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Tinkerwell

Version 4 of Tinkerwell is available now. Get the most popular PHP scratchpad with all its new features and simplify your development workflow today.

Visit Tinkerwell

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy

Lucky Media

Bespoke software solutions built for your business. We ♥ Laravel

Lucky Media

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce

LaraJobs

The official Laravel job board

LaraJobs

Larafast: Laravel SaaS Starter Kit

Larafast is a Laravel SaaS Starter Kit with ready-to-go features for Payments, Auth, Admin, Blog, SEO, and beautiful themes. Available with Vue and Livewire stacks.

Larafast: Laravel SaaS Starter Kit

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector

The latest

View all →

Backpack turns 8 years old, celebrates with 40% discount

Read article

Create a DateTime from a Timestamp With this New Method Coming to PHP 8.4

Read article

Neovim Plugin to for Navigating Laravel and Livewire Components

Read article

Laravel Herd v1.7 is out with updates to the dump UI

Read article

Share Error Package for Laravel's New Exception Page

Read article

Sentry and Laravel announce a new partnership

Read article