Laravel 5.2 .env有时不能及时加载 [英] Laravel 5.2 .env sometimes doesn't load in time

查看:872
本文介绍了Laravel 5.2 .env有时不能及时加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从我的.env文件读取一些可配置文件。这些配置在项目中的各个地方使用。有几次,我收到一个异常,其中一个env变量不存在。示例:



ini_set():依赖系统的时区设置并不安全。您需要使用date.timezone设置或date_default_timezone_set()函数。



在我的.env中,我有:



TIMEZONE =非洲/约翰内斯堡



我的AppServiceProvider我有:



ini_set('date.timezone',getenv('TIMEZONE')); / p>

这就好像.env在我试图使用它的一个变量时没有加载。在应用程序运行期间的各个阶段,我发现有几个不同的.env变量。



编辑



我知道使用配置设置时区,但在这个特定的实例中,我必须使用.env文件,因为我们有一个dev-ops团队负责哪些服务器,我们需要一个支持故障切换,不需要开发人员的干预只是指向另一个服务器。所以我需要知道为什么Laravel似乎有一个问题,加载.env时间而不是工作。

解决方案

p>在同一个项目中,我们最近开始运行一些并发AJAX请求。问题表现为10倍,但同时它让我从不同的角度思考。然后我GoogledLaravel多个AJAX请求错误,发现这: Laravel 5在AJAX密集型应用程序中失去会话和.env配置值



看到这让我意识到我有同样的问题。这不是事实,我使用.env作为可配置,它实际上是两个类似的问题,但具有相同的核心问题:文件读/锁访问。我的会话也起作用,因为我们没有在我们的应用程序中的DB访问,所以我们对从会话变量(文件系统)存储和检索数据有很大的依赖。



我在整个应用程序中使用getenv(),而且如果一个AJAX应用程序的基本HTTP调用有时会失败,



我的解决方案是创建一个单例,在我的AppServiceProvider中触发并存储。 env数据和我的会话数据在对象(内存)。我的理论是,一旦文件已经打开/关闭,即使请求仍然忙,另一个HTTP请求进来的文件将被关闭。从那里,我访问所有我的.env变量和会话数据从单例。现在在罕见的情况下,问题仍然出现。



下一个问题,我肯定会出现是当我们有太多的并发用户加载.env,



我决定写我自己的脚本,打开并读取.env的内容。不是单个问题,因为...所以正如我的链接的帖子中突出显示的,Lance Vucas的PHP dotenv似乎在重型AJAX驱动的项目有问题。它可能是影响这个插件的核心PHP问题。



我从来没有改变任何对我明显的会话问题,自实现自己的脚本会话问题也消失了...


I'm reading some configurables from my .env file. These configurables are used in various places within the project. A few times i've received an exception that one of the env variables does not exist. Example:

ini_set(): It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function.

In my .env I have this:

TIMEZONE=Africa/Johannesburg

In the boot function of my AppServiceProvider I have:

ini_set('date.timezone', getenv('TIMEZONE'));

It's as if the .env hasn't loaded at the time im trying to use one of its variables? Ive seen this happen for a few different .env variables at various stages during the application running.

Edit

I am aware of using config for setting the timezone but in this particular instance I must use the .env file as we have a dev-ops team in charge of which servers we point to + we need a support failover that doesn't require a developer's intervention just to point to another server. So I need to know why Laravel seems to have an issue with loading .env "in-time" instead of work-arounds.

解决方案

On the same project we recently started running some concurrent AJAX requests. The problem manifested 10 fold but at the same time it got me thinking from a different angle. I then Googled "Laravel multiple AJAX requests error" and found this: Laravel 5 losing sessions and .env configuration values in AJAX-intensive applications

Seeing this made me realise I have exactly the same problem. It's not the fact that i'm using the .env as a "configurable", it's actually 2 problems which are similar but have the same core issue: file read/lock access. My sessions were also acting up because we don't have DB access in our application so we have a heavy dependancy on storing and retrieving data from sessions variables (file system). Although im not sure the issue is related to lock files.

I'm using getenv() a lot throughout my application and a basic HTTP call can sometimes fail if an AJAX request is concurrently running as the .env lands up in a locked state i'm guessing.

My solution was to create a singleton that fires in my AppServiceProvider and store the .env data AND my session data in objects (memory). My theory was that once the file has been opened/closed that even if the request was still busy and another HTTP request came in the file would be closed already. From there on-wards I access all my .env variables and session data from the singleton. Now on the rare occasion the issue still appears.

Next issue that i'm sure will come up is when we have too many concurrent users loading the .env but i'll deal with that then.

Update

I decided to write my own script that opens and reads the contents of .env. Not a Single problem since... So as highlighted in my linked post, PHP dotenv by Lance Vucas seems to have issues on heavy AJAX driven projects. It may be a core PHP issue that is affecting this plugin.

I also never changed anything for my apparent session issues and since implementing my own script the session issue has also disappeared...

这篇关于Laravel 5.2 .env有时不能及时加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆