Laravel环境变量5.3设置 [英] Laravel Environment Variables 5.3 setup

查看:90
本文介绍了Laravel环境变量5.3设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,Laravel git会忽略包含环境设置和配置的.env文件.

By default, Laravel git ignores the .env file that contains environment settings and configuration.

我想拥有一个本地.env和一个生产.env可以提交给我的服务器,这样我就完全不需要手动创建生产设置或完全触摸.env文件并且可以使用push部署技术.

I would like to have a local .env, and a production .env that CAN be committed to my server so that I don't need to manually create production settings or touch the .env file at all and can use push to deploy techniques.

有人对此有任何好的解决方案吗?使开发人员始终总是在单个文件中手动创建生产设置,而又不使在生产设置和本地设置之间切换变得容易的做法似乎是一个奇怪的设计决定.我当时正在考虑使用git hooks将.production重命名为.env,但这似乎有点过头了,我想检查一下此功能是否不可用,我已经错过了.

Anyone got any good solution for this? It seems a strange design decision to make developers always manually create the production settings in a single file, and not make it easy to switch between production and local settings. I was considering using git hooks to rename .production to .env but this seems like overkill, and I want to check that this functionality isn't available out of the box and I've missed it.

奖金点说明了为什么拥有三个文件.local.env.production.env.env(在两个文件之间切换)是一个坏主意,而原始团队则避免了.

Bonus points for explaining why having three files .local.env, .production.env and .env (which switches between the two) files is a bad idea, and one the original team avoided.

我正在使用最新版本的Laravel(5.3).

I'm using the latest version of Laravel (5.3).

推荐答案

此设计背后的原因主要是:

The reasons behind this design are mainly:

  • 安全问题
  • 不同的环境可能需要不同的设置

Laravel利用了Vance Lucas的 DotEnv PHP库来提供.env功能,因此您可以在此处以及在Laravel官方文档中阅读有关此内容的更多信息:

Laravel makes use of the DotEnv PHP library by Vance Lucas to provide the .env functionality, so you can read more about it there as well as in the officiall Laravel docs:

您的.env文件不应提交给应用程序的源 控制,因为每个使用您的应用程序的开发人员/服务器都可以 需要不同的环境配置.

Your .env file should not be committed to your application's source control, since each developer / server using your application could require a different environment configuration.

如果您与团队一起发展,则不妨继续 您的应用程序中的.env.example文件.通过放置占位符 配置文件示例中的值,您的其他开发人员 团队可以清楚地看到需要运行哪些环境变量 您的应用程序.

If you are developing with a team, you may wish to continue including a .env.example file with your application. By putting place-holder values in the example configuration file, other developers on your team can clearly see which environment variables are needed to run your application.

来自DotEnv:

.env文件通常不受版本控制,因为它可以 包含敏感的API密钥和密码.一个单独的.env.example文件 使用定义的所有必需环境变量创建 对于敏感的,要么由用户自己提供 开发环境或在其他地方传达给项目 合作者.然后,项目合作者可以独立复制 将.env.example文件保存到本地.env,并确保所有设置均为 根据当地环境进行更正,填写密钥或 在必要时提供自己的价值.在这种用法中,.env 文件应添加到项目的.gitignore文件中,以便它将 永远不要由合作者犯下.这种用法可确保没有 敏感密码或API密钥将永远存在于版本控制中 历史记录,因此发生安全漏洞和生产的风险较小 价值将永远不必与所有项目合作者共享.

The .env file is generally kept out of version control since it can contain sensitive API keys and passwords. A separate .env.example file is created with all the required environment variables defined except for the sensitive ones, which are either user-supplied for their own development environments or are communicated elsewhere to project collaborators. The project collaborators then independently copy the .env.example file to a local .env and ensure all the settings are correct for their local environment, filling in the secret keys or providing their own values when necessary. In this usage, the .env file should be added to the project's .gitignore file so that it will never be committed by collaborators. This usage ensures that no sensitive passwords or API keys will ever be in the version control history so there is less risk of a security breach, and production values will never have to be shared with all project collaborators.

总而言之,如果您确定只部署到一个系统,并且设置将始终相同,并且.env内没有敏感数据(或者您可以同意参与的每个人都可以使用该数据) ,那么您可能可以继续进行解决方案了.

In summary, if you're certain that you will deploy to only one system and settings will always be the same, and there is no sensitive data inside the .env (or you're OK with everyone involved seeing it), then you could probably go ahead with your solution.

作为辅助节点:自从您明确要求设计决策以来,DotEnv显然受到了十二要素应用程序的启发,该应用程序倡导将配置与代码严格隔离以及其他因素.

As a side node: Since you were asking for design decisions specifically, DotEnv was apparently inspired by The Twelve-Factor App, which advocates strict separation of config from code among other factors.

这篇关于Laravel环境变量5.3设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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