Laravel 5.1 如何改变环境?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31489626/
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 11:57:59  来源:igfitidea点击:

How to change the environment in Laravel 5.1?

phplaravelenvironment-variablesproduction-environmenttest-environments

提问by Homo Sapien

What I understand about the working of environments in Laravel is that you have different environments for different, well environments. So, if you are running your app locally you could have a .env.localfile. If you are testing or on production, you could use .env.testingor .env.production. (Correct me if I am wrong.)
By default we get .envfile that we can edit. But can anybody tell me what is the workflow of changing the environments in Laravel. I tried the documentation but I couldn't get it. Please help me.

我对 Laravel 中环境工作的理解是,对于不同的、良好的环境,您有不同的环境。所以,如果你在本地运行你的应用程序,你可以有一个.env.local文件。如果您正在测试或生产,您可以使用.env.testing.env.production(如果我错了,请纠正我。)
默认情况下,我们会获得.env可以编辑的文件。但是谁能告诉我在 Laravel 中更改环境的工作流程是什么。我尝试了文档,但无法获取。请帮我。

回答by Mohamed Salem Lamiri

When you install Laravel 5.1 you get two files .envand .env.exampleif you want to work locally you set :

当你安装 Laravel 5.1 时,你会得到两个文件.env.env.example如果你想在本地工作,你可以设置:

APP_ENV=local
APP_DEBUG=true

in prod you set

在你设置的产品中

APP_ENV=production
APP_DEBUG=false

An error message in debug mode

调试模式下的错误消息

enter image description here

在此处输入图片说明

An error message from production mode

来自生产模式的错误消息

enter image description here

在此处输入图片说明

Note: you have two .env files .envand .env.example.. you can also create another one that you name .env.productionbut keep in mind that in order to get your configuration loaded you must just rename your file to .env

注意:您有两个 .env 文件.env.env.example.. 您也可以创建另一个.env.production您命名的文件,但请记住,为了加载您的配置,您必须将您的文件重命名为.env

EDIT :So in case you are still working in local and you need another database for test, you can create another file so in total you have 3 .env files :

编辑:因此,如果您仍在本地工作并且需要另一个数据库进行测试,则可以创建另一个文件,因此总共有 3 个 .env 文件:

.env.production
.env.local1
.env.local2

whenever you want to switch configuration just rename the desired file to .env

每当您想切换配置时,只需将所需文件重命名为 .env

回答by Almazik G

The idea of having .env.local.php, .env.production.phphas been deprecated since Laravel 5. Now, in L5, we have single .envfile, where you store all your environment configuration. To define your environment, you should put APP_ENV=localto this file.

从 Laravel 5 开始,拥有.env.local.php,的想法.env.production.php已被弃用。现在,在 L5 中,我们有一个.env文件,您可以在其中存储所有环境配置。要定义您的环境,您应该放入APP_ENV=local此文件。

Once you deploy your project on production, you would create .envfile on the server and define APP_ENV=production

在生产上部署项目后,您将.env在服务器上创建文件并定义APP_ENV=production

If you use service like Laravel Forge, it provides you nice simple way of storing your environment data. But that's another story:)

如果您使用 Laravel Forge 之类的服务,它为您提供了一种非常简单的存储环境数据的方法。但那是另一个故事了:)

Edit

编辑

to make use of several db connections you might do the following:

要使用多个数据库连接,您可以执行以下操作:

in your config/database.phpfile

在你的config/database.php文件中

<?php
return array(

'default' => env('DEFAULT_DB_CONNECTION', 'mysql'),

'connections' => array(

    # Our primary database connection
    'mysql' => array(
        'driver'    => 'mysql',
        'host'      => 'host1',
        'database'  => 'database1',
        'username'  => 'user1',
        'password'  => 'pass1'
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

    # Our secondary database connection
    'another_mysql' => array(
        'driver'    => 'mysql',
        'host'      => 'host2',
        'database'  => 'database2',
        'username'  => 'user2',
        'password'  => 'pass2'
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),
),

);

);

And then, in the .envfile put another key

然后,在.env文件中放另一个键

DEFAULT_DB_CONNECTION=another_mysql

Of course, this sort of predefines your connection. If you want to be dynamic, you can do the following

当然,这种预定义您的连接。如果要动态,可以执行以下操作

$users = DB::connection('another_db_connection')->select('users somehow');

that way you would get results from your secondary mysql connection, no matter what is set up in your environment

这样你就可以从你的辅助 mysql 连接中得到结果,无论你的环境设置了什么