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

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

Laravel Daily

1 год назад

16,105 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

enmaboya
enmaboya - 19.01.2023 19:22

thanks!

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

Ответить
Guido Walter Pettinari
Guido Walter Pettinari - 06.10.2022 14:32

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

Ответить
René Eriksen Sinnbeck
René Eriksen Sinnbeck - 12.08.2022 08:40

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 :)

Ответить
Krishna Shrestha
Krishna Shrestha - 11.08.2022 08:52

What about in FrontEnd?

Ответить
JustTestingMyCurrentSkills
JustTestingMyCurrentSkills - 10.08.2022 15:16

Damn, I wish I knew.

Ответить
Harsha Subasinghe
Harsha Subasinghe - 09.08.2022 08:38

What would be the best approach to keeping global variables, let's say constants. I've seen people recommending keeping a custom config file named "constants". Is there a better bulletproof approach than that?

Ответить
YT-JR
YT-JR - 08.08.2022 21:08

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.

Ответить
Max Romagnoli
Max Romagnoli - 08.08.2022 16:31

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

Ответить
René Eriksen Sinnbeck
René Eriksen Sinnbeck - 08.08.2022 09:47

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

Ответить
BoraN7
BoraN7 - 08.08.2022 00:55

Some people say that you should not send the .env file to the server. Is it a security risk?

Ответить
Farid Rouah
Farid Rouah - 07.08.2022 23:38

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.

Ответить
Ossama Khayat
Ossama Khayat - 07.08.2022 22:43

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

Ответить
Simarpal Singh
Simarpal Singh - 07.08.2022 21:50

Great 👍🏻 teacher

Ответить
WAID: ios,android,web,development
WAID: ios,android,web,development - 07.08.2022 17:14

Hello dear author, i bought filament course but, in first lesson, after instaling, i got css error. How to fix it

Ответить
Bernardo Campos
Bernardo Campos - 07.08.2022 16:50

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

Ответить
Thomas A. Anderson
Thomas A. Anderson - 07.08.2022 14:03

you're the man 👏👏👏👏👏👏👏👏

Ответить
ありますMK
ありますMK - 07.08.2022 12:48

thanks, very helpful

Ответить
padvk
padvk - 07.08.2022 12:47

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

Ответить
Dale Ryan Aldover
Dale Ryan Aldover - 07.08.2022 12:27

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.

Ответить
JfordGaming
JfordGaming - 07.08.2022 11:46

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

Ответить
Stoney_Eagle
Stoney_Eagle - 07.08.2022 11:16

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

Ответить
Raphael D
Raphael D - 07.08.2022 10:34

hi Mauricio Pochettino

Ответить
Gameplay By Faks
Gameplay By Faks - 07.08.2022 10:33

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)

Ответить
Jin
Jin - 07.08.2022 10:19

thx

Ответить
Ali Neisi
Ali Neisi - 07.08.2022 10:03

short trick and super informative
thx ❤️🔥

Ответить
Ricko
Ricko - 07.08.2022 09:12

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

Ответить
Sayd Fuad
Sayd Fuad - 07.08.2022 08:29

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.

Ответить
yash khuthia
yash khuthia - 07.08.2022 08:14

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

Ответить