Laravel 11 streamlined configuration files

Published on by

Laravel 11 streamlined configuration files image

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 logo

Laravel Forge

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

Laravel Forge
Tinkerwell logo

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 logo

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 logo

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 logo

Lucky Media

Bespoke software solutions built for your business. We ♥ Laravel

Lucky Media
Lunar: Laravel E-Commerce logo

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 logo

LaraJobs

The official Laravel job board

LaraJobs
Larafast: Laravel SaaS Starter Kit logo

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 VILT and TALL stacks.

Larafast: Laravel SaaS Starter Kit
SaaSykit: Laravel SaaS Starter Kit logo

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 logo

Rector

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

Rector

The latest

View all →
DirectoryTree Authorization is a Native Role and Permission Management Package for Laravel image

DirectoryTree Authorization is a Native Role and Permission Management Package for Laravel

Read article
Sort Elements with the Alpine.js Sort Plugin image

Sort Elements with the Alpine.js Sort Plugin

Read article
Anonymous Event Broadcasting in Laravel 11.5 image

Anonymous Event Broadcasting in Laravel 11.5

Read article
Microsoft Clarity Integration for Laravel image

Microsoft Clarity Integration for Laravel

Read article
Apply Dynamic Filters to Eloquent Models with the Filterable Package image

Apply Dynamic Filters to Eloquent Models with the Filterable Package

Read article
Property Hooks Get Closer to Becoming a Reality in PHP 8.4 image

Property Hooks Get Closer to Becoming a Reality in PHP 8.4

Read article