Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Using Configs in Laravel 8
Graham Morby
Graham Morby

Posted on

     

Using Configs in Laravel 8

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"
Enter fullscreen modeExit fullscreen mode

We grab that in blade or a controller as so

env('MAIL_SUPPORT_ADDRESS')
Enter fullscreen modeExit fullscreen mode

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

<?PHPreturn=>['email'=>env('MAIL_SUPPORT_ADDRESS','help@grahammorbydev.com')]
Enter fullscreen modeExit fullscreen mode

Then in my blade file or controller, I just use

config('contact.email');
Enter fullscreen modeExit fullscreen mode

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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Hey guys, 15-year developer! I code with Vue.js and Python, I love to learn but also love to teach! So I try and write informative tutorials and posts. I am new to blogging and would love any feedback
  • Location
    Portsmouth UK
  • Work
    Senior Developer Leighton
  • Joined

More fromGraham Morby

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp