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
How to change the environment in Laravel 5.1?
提问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.local
file. If you are testing or on production, you could use .env.testing
or .env.production
. (Correct me if I am wrong.)
By default we get .env
file 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 .env
and .env.example
if 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
调试模式下的错误消息
An error message from production mode
来自生产模式的错误消息
Note: you have two .env files .env
and .env.example
.. you can also create another one that you name .env.production
but 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.php
has been deprecated since Laravel 5. Now, in L5, we have single .env
file, where you store all your environment configuration. To define your environment, you should put APP_ENV=local
to 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 .env
file 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.php
file
在你的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 .env
file 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 连接中得到结果,无论你的环境设置了什么