Laravel 中需要什么 env 和 env.example 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52084447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:00:59  来源:igfitidea点击:

What is the need for env and env.example files in Laravel?

phplaravel

提问by Mirich

I am new to Laravel and want a simple explanation of the .env and .env.example files, why we need them and the difference between them.

我是 Laravel 的新手,想要简单解释 .env 和 .env.example 文件,为什么我们需要它们以及它们之间的区别。

I know that .env is used to specify the app's database connection, for example but I would like to understand it deeper.

例如,我知道 .env 用于指定应用程序的数据库连接,但我想更深入地了解它。

回答by Josh

The .env file stores configuration variables for your application and .env.example is simply an example of what might be in the .env file! You can easily rename .env.example to .env to get started.

.env 文件存储您的应用程序的配置变量,而 .env.example 只是 .env 文件中可能包含的内容的一个示例!您可以轻松地将 .env.example 重命名为 .env 以开始使用。

What are configuration variables? From The Twelve-Factor App

什么是配置变量?来自十二因素应用程序

An app's config is everything that is likely to vary between deploys (staging, production, developer environments, etc). This includes:

  • Resource handles to the database, Memcached, and other backing services
  • Credentials to external services such as Amazon S3 or Twitter
  • Per-deploy values such as the canonical hostname for the deploy

应用程序的配置是可能因部署(暂存、生产、开发人员环境等)而异的所有内容。这包括:

  • 数据库、Memcached 和其他支持服务的资源句柄
  • 外部服务的凭据,例如 Amazon S3 或 Twitter
  • 每个部署的值,例如部署的规范主机名

In Laravel the .env file also contains your app key which is used for encryption in your app. Because of this, and because you will likely store other private keys in this file, ensure you do not commit .env to your source control or share it publicly!

在 Laravel 中,.env 文件还包含您的应用密钥,用于在您的应用中进行加密。因此,并且因为您可能会在此文件中存储其他私钥,请确保不要将 .env 提交到您的源代码管理或公开共享!

I recommend you read the link above for an explanation of why you should separate configuration from your application and for Laravel-specific information you can look here

我建议您阅读上面的链接,了解为什么应该将配置与应用程序分开,以及 Laravel 特定的信息,您可以在这里查看

回答by Thiago B. Bittencourt

.envfile, as its name suggest, is a local where you put all your environment setup, such as database credentials, cache drivers and etc. Everything that is about the server that the project is running, and may have different values for different servers, are setup here.

.env文件,顾名思义,是您放置所有环境设置(例如数据库凭据、缓存驱动程序等)的本地文件。有关项目运行的服务器的所有内容,对于不同的服务器可能具有不同的值, 在这里设置。

Per example, your local dev environment has different database credentials than production environment. Also your colleague dev environment has different than yours. So each one has a .envwith different informations.

例如,您的本地开发环境与生产环境具有不同的数据库凭据。您的同事开发环境也与您的不同。所以每个人都有一个包含不同信息的.env

And because of these, this file can't be versioned, so .env.exampleis the file that has every constants setups that .envhas but with no values, and only this one is versioned. .env.exampleworks as a guide for creating a .envfile with the needed informations that is needs to have the application running.

正因为如此,这个文件不能被版本化,所以.env.example是一个文件,它包含.env 的所有常量设置,但没有值,只有这个是版本化的。.env.example用作创建.env文件的指南,其中包含运行应用程序所需的信息。

As you are working with Laravel, you can find more informations here: environment-configuration

当您使用 Laravel 时,您可以在此处找到更多信息:环境配置

回答by Michael

.env is simply used to store all sensitive files like password API key, database,and so on as environment variables to be used in your code later this sensitive files are not included in the code base and will not be there when been pushed to git. .env.example This is a file that tells other programmer what is meant to be in there code when your code is cloned or been used by another user.

.env 仅用于存储所有敏感文件,如密码 API 密钥、数据库等,作为稍后在代码中使用的环境变量,此敏感文件不包含在代码库中,并且在推送到 git 时也不存在. .env.example 这是一个文件,当您的代码被克隆或被其他用户使用时,它会告诉其他程序员代码中的内容。

Example .env API_KEY="hwhhwhshs6585gahwhgwuwjwusuhs"

示例 .env API_KEY="hwhhwhshs6585gahwhgwuwjwusuhs"

.env.example API_KEY="YOUR API KEY GOES HERE"

.env.example API_KEY="您的 API 密钥在此处"

回答by Waqar

.env file contains various settings, one row – one KEY=VALUE pair. And then, within your Laravel project code you can get those environment variables with function env(‘KEY').

.env 文件包含各种设置,一行 – 一对 KEY=VALUE。然后,在您的 Laravel 项目代码中,您可以使用函数 env('KEY') 获取这些环境变量。

The rule is that .env file is not committed to the repository, so it is really convenient, cause then people on your team can change their variables locally without committing them to the repository.

规则是 .env 文件不提交到存储库,所以它真的很方便,因为这样你团队中的人可以在本地更改他们的变量,而无需将它们提交到存储库。

Now, .env.example file, on the contrary, is included in the repository – it is used as an example file for you to know what KEY=VALUE pairs you need for your project. Most often it is used to copy it to .env file and then change the values.

现在,相反,.env.example 文件包含在存储库中 - 它用作示例文件,让您了解您的项目需要哪些 KEY=VALUE 对。大多数情况下,它用于将其复制到 .env 文件,然后更改值。

You can also read about it in the official Laravel documentation.

你也可以在 Laravel 官方文档中阅读它。

回答by Pedro Caseiro

The .env.example file is just an example of the .env file. It is not used by the app. It's used to serve as a base for you to edit and rename.

.env.example 文件只是 .env 文件的一个例子。应用程序不使用它。它用作您编辑和重命名的基础。

The .env file contains constants that are specific to that application to those environments. What this means is for example if I want to deploy my app in multiple places with the same code, I'll just have to change some settings on this file to run on each environment and we are all set, no code changes needed.

.env 文件包含特定于这些环境的应用程序的常量。这意味着例如,如果我想使用相同的代码在多个地方部署我的应用程序,我只需要更改此文件的一些设置即可在每个环境中运行,我们都已设置完毕,无需更改代码。

These settings can be database connection settings but they can be used for other things too like the APP_KEY that should be different for each application and used my many functions.

这些设置可以是数据库连接设置,但它们也可以用于其他事情,例如 APP_KEY 应该对每个应用程序都不同并使用我的许多功能。

Be careful, this .env file should not be shared anywhere as it contains private information about that specific deploy.

请注意,此 .env 文件不应在任何地方共享,因为它包含有关该特定部署的私人信息。

You can read more here: https://laravel.com/docs/5.6/configuration#environment-configuration

您可以在此处阅读更多信息:https: //laravel.com/docs/5.6/configuration#environment-configuration

回答by Christian Gallarmin

From the version Laravel 5.0 in your main folder you should have .env file which contains various settings, one row – one KEY=VALUE pair. And then, within your Laravel project code you can get those environment variables with function env(‘KEY').

从主文件夹中的 Laravel 5.0 版本开始,您应该有 .env 文件,其中包含各种设置,一行 – 一对 KEY=VALUE。然后,在您的 Laravel 项目代码中,您可以使用函数获取这些环境变量env(‘KEY').

The rule is that .envfile is not committed to the repository, so it is really convenient, cause then people on your team can change their variables locally without committing them to the repository.

规则是.env文件不提交到存储库,所以这真的很方便,因为这样你团队中的人可以在本地更改他们的变量,而无需将它们提交到存储库。

Now, .env.examplefile, on the contrary, is included in the repository – it is used as an example file for you to know what KEY=VALUE pairs you need for your project. Most often it is used to copy it to .envfile and then change the values.

现在,.env.example相反,文件包含在存储库中 - 它用作示例文件,让您了解您的项目需要哪些 KEY=VALUE 对。大多数情况下,它用于将其复制到.env文件,然后更改值。

Your .envfile should not be committed to your application's source control, since each developer / server using your application could require a different environment configuration. Furthermore, this would be a security risk in the event an intruder gains access to your source control repository, since any sensitive credentials would get exposed.

您的.env文件不应提交给应用程序的源代码管理,因为使用您的应用程序的每个开发人员/服务器可能需要不同的环境配置。此外,如果入侵者获得对您的源控制存储库的访问权,这将是一个安全风险,因为任何敏感凭据都将被暴露。

If you are developing with a team, you may wish to continue including a .env.examplefile 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. You may also create a .env.testingfile. This file will override the .envfile when running PHPUnit tests or executing Artisan commands with the --env=testing option.

如果您正在与团队一起开发,您可能希望继续.env.example在您的应用程序中包含一个文件。通过在示例配置文件中放置占位符值,您团队中的其他开发人员可以清楚地看到运行您的应用程序需要哪些环境变量。您也可以创建一个.env.testing文件。.env当运行 PHPUnit 测试或执行 Artisan 命令时,此文件将覆盖该文件--env=testing option.