Ovear's Blog

I'am Ovear,Ovear is me.

Theme Refrain made by Eiko

Proudly powered by WordPress

解决Laravel获取获取配置不稳定的问题

(还在更新排版中...)
原因
在Apache开启worker或者event配合PHP Thread-safe版本的多线程情况下,环境变量由各个PHP线程共享。
在前一个线程初始化/退出清理环境的时候,会影响到其他线程读取环境变量,导致phpdotenv无法获取到正确的值(被初始化),从而导致Laravel获取不到设定的config,导致config初始化。

解决方法
建立Config cache,就不用去调用DotEnv了

php artisan config:clear
php artisan config:cache

参考资料
https://lialosiu.com/2016/01/laravel-%e5%9c%a8%e6%9e%81%e9%ab%98%e7%9a%84%e7%9e%ac%e9%97%b4%e5%b9%b6%e5%8f%91%e6%97%b6%e5%87%ba%e7%8e%b0%e7%9a%84%e9%97%ae%e9%a2%98/
https://laracasts.com/discuss/channels/general-discussion/env-not-reading-variables-sometimes
https://github.com/vlucas/phpdotenv/issues/160
https://bugs.php.net/bug.php?id=71607
https://bugs.php.net/bug.php?id=74986
https://laracasts.com/discuss/channels/general-discussion/issue-with-parallel-requests

解决Laravel获取获取配置不稳定的问题有 2 条评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

© 2026 Ovear's Blog All rights reserved.