One of the biggest things we do in modern web development is to consume API's and with that comes lots of keys, secrets, and config points. With that comes issues, now up until recently I would store the needed key etc in my .env and call it direct! Which worked and worked well.
So let's say we have a support email that is used in many places, we store that in the .env file we call it.
MAIL_SUPPORT_ADDRESS: "support@grahammorbydev.com"
We grab that in blade or a controller as so
env('MAIL_SUPPORT_ADDRESS')
Which works just fine, but let's say you go into production, do not update your .env, and load the site? What will happen? That page will error that the address doesn't exist or the support email will not work. Now, this is a really basic use case for config, but I hope it will allow you to see the benefit.
So what we do is head over to the config folder and create a new file called contact.php. if we then add the following code
<?PHP
return => [
'email' => env('MAIL_SUPPORT_ADDRESS', 'help@grahammorbydev.com')
]
Then in my blade file or controller, I just use
config('contact.email');
This allows me to use any value I set in my env or fallback should I forget and that's just the start. There are tons of other things you can do to allow you to write cleaner code and give you the edge in Laravel!
Top comments (0)