logo
podcast Podcast
get help Get Unstuck

Where should you put extra config values?

Make future Laravel upgrades easier

Joel Clermont
Joel Clermont
2024-10-30

At last count, a default Laravel install includes 10 files in the config directory.

My goal is to edit these as little as possible. The vast majority of these settings look at an environment variable, so that is my default way to customize anything.

But what about some additional piece of configuration you want to store? Where should that go?

Here is my thought process for where to put extra config values:

If it's a piece of data specifically for this app, I create a new file named after the app. For example, on this site, we have some external links that we frequently use and store those in a links array in config/mastering-laravel.php

If the config value is for a specific external service, (Twilio, Fathom Analytics, etc.), then I put it in config/services.php. That is exactly what the file is designed for. But I make sure to keep my additional keys at the bottom of the file, with all the Laravel default keys in their original position.

Another common use case is needing additional instances of a config section. For example, maybe you have multiple databases or cache stores. So instead of just a single mysql database config, you have mysql-read and mysql-write.

In this case, I leave the default mysql untouched, but then create the two new named configs directly under the default. This avoids merge conflicts in the future when Laravel makes default config changes, and it also makes it easy to compare so our customized sections stay in sync with Laravel defaults too.

With these simple rules, I've upgraded many apps across multiple major Laravel versions with minimal config changes.

Here to help,

Joel

P.S. Are you struggling to upgrade your app to a modern version of Laravel or PHP? We can definitely help.

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼
email
No spam. Only real-world advice you can use.