Why You Should NOT Use env() in Laravel Controllers?

preview_player
Показать описание
One tip about environments and configuration caching in Laravel.

- - - - -
Support the channel by checking out my products:

- - - - -
Other places to follow:
Рекомендации по теме
Комментарии
Автор

I already knew that, I've learned the hard way... after 4hrs of debuggin 😅

ricko
Автор

Nice! + Also you can create your own config file too instead of putting them in config.php if you have a lot of custom configuration fields.

saydfuad
Автор

Your tips helped me out when I was starting out with laravel development. Especially those old articles you have on your website. I've been recommending your channel ever since.

daleryanaldover
Автор

On every deployment, i run the command:
php artisan optimize:clear
Which clears:
Cached config
Cached views
Cached routes
...
or run the command:
php artisan optimize
which clears all the cached config, views...
And create a new cache for all.

faridrouah
Автор

Thank you for everything! The channel's amazing!

thja
Автор

Thank you, Povilas. I've always know it was best practice to use config and only ever use the env() function from within your configuration files, but I never bothered to question why that was and see demonstrations of issues it can lead to. One thing your video has taught me is that I should start using config cache on my applications and will be doing so in the near future.

jrplops
Автор

A classic case of premature optimization. I wish the Laravel team made the config cache optional, instead of loading it by default.

guidowalterpettinari
Автор

Aaaahh .. now I know why everytime I use artisan config:cache I hit the wall 😂 Thank for the tip! 👍

ossamakhayat
Автор

env, tends to be empty due to cache and cause hell of fun :)) (if recall correctly this issue appeared in laravel 7 or 8, since in core were changed introduced)

GamePlayByFaks
Автор

I used to use env until laravel started to cache by default and it broke everything 😂😂

Stoney_Eagle
Автор

Awesome. I see people do this constantly. I will link to this video instead of the docs from now on 👍

resin
Автор

Why doesn't the env function load the cached value when used in a controller? Is that purposeful design?

padvk
Автор

You convinced me to use config instead of env out of laziness

MaxRomagnoli
Автор

Can I recommend renaming the video to "Why you should not use env() outside of Laravel config files?". The same problem is present in all files except config files :)

resin
Автор

thanks!

what about the unit / feature tests?
do we use only config() there, too ?

enmaboya
Автор

yeah I've experience with this implementation before must be in config.

jfordgaming
Автор

How much faster goes the app after running config:cache?🤔

bernardocampos
Автор

short trick and super informative
thx ❤️🔥

llBestBoyll
Автор

So, here's a question. Just how much of a performance boost is gained when caching the values compared to using the env() function? I've seen this claim many times but not a single instance (yet) of someone testing it.

I'm an engineer, I need proof. You (and others) say it's faster? Prove it!

Yes, I know I can do that myself but if you or someone on Youtube carries out the test, then it will be available for all to see.

tetleyk
Автор

This is helpful, i will give it a try and see where it's getting stored and the time difference between cached vs non-cached config for our one of the full-blown project

yashkhuthia