laravel 变量符_了解Laravel环境变量

laravel 变量符

With the introduction of Laravel 5 and the adoption PHPDotenv, Laravel environment variables have become easier to use than ever before. In today's tutorial I’ll walk you through how to get started using environment variables in your project.

随着Laravel 5的引入和PHPDotenv的采用,Laravel环境变量变得比以往任何时候都更易于使用。 在今天的教程中,我将指导您如何开始在项目中使用环境变量

Luckily the process is relatively simple for anyone familiar with other frameworks that utilize environment variables or anyone with a basic knowledge of Laravel.

幸运的是,对于熟悉使用环境变量的其他框架的任何人或具有Laravel基本知识的任何人,该过程都相对简单。

为什么要使用环境变量 (Why You Should Use Environment Variables)

Environment variables allow developers to extract sensitive credentials from their source code and to use different configuration variables based on their working environment.

环境变量允许开发人员从其源代码中提取敏感凭据,并根据其工作环境使用不同的配置变量。

For most developers their local machine has different database credentials than their production environment. While different database credentials are one of the most common differences between production and local environments there are a host of other configuration variables that may also differ.

对于大多数开发人员而言,其本地计算机与生产环境具有不同的数据库凭据。 尽管不同的数据库凭据是生产环境与本地环境之间最常见的差异之一,但还有许多其他配置变量也可能有所不同。

Some environment variables that could differ are:

一些可能不同的环境变量是:

  • We want to use Mailtrap to send test emails locally and use Mandrill to send real emails on our production server

    我们想使用Mailtrap在本地发送测试电子邮件,并使用Mandrill在我们的生产服务器上发送真实的电子邮件
  • We have different Facebook Developer Credentials for local vs production apps

    对于本地应用程序和生产应用程序,我们有不同的Facebook开发人员凭据
  • We want to use file for Laravel sessions locally and redis for sessions on production

    我们想在本地Laravel会话中使用file ,而在生产中使用redis会话

You will most likely also have a list of outside services your application connects to. Environment variables offer a way for you to extract these from your application and even vary them based on your environment. We don't want to keep security credentials in the git repo.

您很可能还会获得应用程序连接到的外部服务的列表。 环境变量为您提供了一种从应用程序中提取它们甚至根据环境进行变化的方法。 我们不想将安全凭证保留在git repo中。

Environment variables can also be extremely helpful when creating and working on a testing environment. With relative ease developers can switch between testing and production environments. Now that you understand some of the benefits of environment variables we can take look at how to implement them In your application.

在创建和处理测试环境时,环境变量也可能非常有用。 开发人员可以相对轻松地在testingproduction环境之间切换。 现在您已经了解了环境变量的一些好处,我们可以看看如何在应用程序中实现它们。

在您的应用程序中声明环境变量 (Declaring Environment Variables in Your App)

A successful Laravel install will include a .env example file in your application's root folder.

成功的Laravel安装将在应用程序的根文件夹中包含一个.env示例文件。

laravel-env

If you installed Laravel with Composer than your .env.example file will have already been renamed to .env, if you did not install through composer then go ahead and rename your file.

如果您使用Composer安装Laravel,则您的.env.example文件将已经重命名为.env ,如果您不是通过composer安装的,请继续并重命名您的文件。

If you are working in a team environment it may help to leave the .env.example file and use it as a way to show what environment variables are needed for the application to run.

如果您在团队环境中工作,则可能需要保留.env.example文件并将其用作显示应用程序运行所需的环境变量的一种方式。

保持.env不受源代码控制 (Keep .env Out of Source Control)

Your .env file should be kept out of your version control system. If you are using Git then add the .env file to the .gitignore file so that it will not be committed. Laravel handles this automatically for us in the default .gitignore file.

您的.env文件应保留在版本控制系统之外。 如果您使用的是Git,则将.env文件添加到.gitignore文件中,以使其不会被提交。 Laravel在默认的.gitignore文件中为我们自动处理此问题。

Your .env file should resemble something similar to this.

您的.env文件应类似于以下内容。

APP_ENV=local
APP_DEBUG=true
APP_KEY=SomeRandomString

DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

数据库凭证 (Database Credentials)

For this example we will work through how to change your database configuration based on your environment. We will start by taking a look at config/database.php. This file holds the information necessary to connect to your database.

对于此示例,我们将研究如何根据您的环境更改数据库配置。 我们将从看config/database.php 。 该文件包含连接到数据库所需的信息。

'mysql' => [
    'driver'    => 'mysql',
    'host'      => env('DB_HOST', 'localhost'),
    'database'  => env('DB_DATABASE', 'forge'),
    'username'  => env('DB_USERNAME', 'forge'),
    'password'  => env('DB_PASSWORD', ''),
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'strict'    => false,
],

Notice that most of the variables are set with the env() function. env() is one of the built-in helper functions in Laravel.

请注意,大多数变量是使用env()函数设置的。 env()是Laravel中的内置辅助函数之一。

env() takes two parameters,

env() 有两个参数

  1. Name of the environment variable in your .env file

    .env文件中环境变量的名称
  2. Default value if the environment variable cannot be found.

    如果找不到环境变量,则为默认值。

For example if your config/databse.php MySql section and your .env file were to resemble the code snippets below your app would first look for DB_HOST in the .env file and since it was declared then set host in the mysql array equal to myHost.

例如,如果你config/databse.php MySQL的部分,你.env文件是类似于您的应用程序下面的代码段将首先寻找DB_HOST.env文件,因为它被宣称然后设置主机mysql的数组中等于myHost

// config/database.php

'mysql' => [
    'driver'    => 'mysql',
    'host'      => env('DB_HOST', 'localhost'),
    'database'  => env('DB_DATABASE', 'forge'),
    'username'  => env('DB_USERNAME', 'forge'),
    'password'  => env('DB_PASSWORD', ''),
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'strict'    => false,
],
// .env

DB_HOST=myHost
DB_DATABASE=myDB
DB_USERNAME=myUsername
DB_PASSWORD=mySecret

If there was no DB_HOST environment variable then it would default to localhost. Since your .env file is not in your version control system you will need to create a .env file in your production environment.

如果没有DB_HOST环境变量,那么它将默认为localhost 。 由于您的.env文件不在版本控制系统中,因此您需要在生产环境中创建一个.env文件。

在Forge中设置生产环境变量 (Setting Production Environment Variables in Forge)

When creating servers on Laravel Forge, there is an easy way to set these environment variables.

Laravel Forge上创建服务器时,有一种简单的方法来设置这些环境变量。

To set your environment variables in forge click on your server and then click manage next to your site's name. Now click on the environment tab and you should see something similar to below.

要在forge中设置环境变量,请在服务器上单击,然后单击站点名称旁边的管理。 现在,单击环境选项卡,您应该会看到类似下面的内容。

laravel-forge-environment-variables

Once you click edit environment, you'll be able to see the full .env file that is stored on the server. Edit freely!

单击编辑环境后,您将可以查看服务器上存储的完整.env文件。 自由编辑!

If you click edit environment you can now go ahead and add in your environment variables.

如果单击“编辑环境”,现在可以继续添加环境变量。

其他服务器 (Other Servers)

If you are running your server with amazon then the process is a little less user friendly. You can start by using the SSH command to connect to your server and then running vim .env or sudo vim .env depending on your user privileges. Make sure to run this command in the root of your laravel applications folder. By running vim .env you are creating a new file named .env and then opening it in the amazing Vim editor.

如果您正在使用Amazon运行服务器,则该过程对用户的友好程度会降低一些。 您可以先使用SSH命令连接到服务器,然后根据您的用户权限运行vim .envsudo vim .env 。 确保在laravel应用程序文件夹的根目录中运行此命令。 通过运行vim .env您将创建一个名为.env的新文件,然后在令人惊叹的Vim编辑器中将其打开。

Vim is a text based editor for Unix environments and is relatively simple to pick up with a little practice. If you are a novice you can add text with i and then when you are finished hit the escape button followed by :wq. the : informs vim you are running a command and wq tells Vim to save and quit.

Vim是用于Unix环境的基于文本的编辑器,通过一点实践就可以很容易地掌握。 如果您是新手,则可以使用i添加文本,然后在完成操作后单击escape按钮,然后按:wq:通知vim您正在运行命令,而wq告诉Vim保存并退出。

结论 (Conclusion)

Hopefully you now have a much better understanding of environment variables in Laravel. Environment variables are essential tools for anyone looking to develop in an MVC framework across multiple environments.

希望您现在对Laravel中的环境变量有了更好的了解。 对于希望在多个环境中的MVC框架中进行开发的任何人,环境变量都是必不可少的工具。

Not only are you able to separate sensitive data from your application code, but you will also save time switching between local and production environments.

您不仅可以从应用程序代码中分离出敏感数据,而且还可以节省在本地和生产环境之间切换的时间。

Feel free to reach out with any comments or questions below. Happy Coding!

欢迎与以下任何评论或问题联系。 编码愉快!

翻译自: https://scotch.io/tutorials/understanding-laravel-environment-variables

laravel 变量符

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值