如何使用 laravel artisan 设置 env 以拥有两个不同的数据库连接(本地/远程)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14343627/
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 set env with laravel artisan to have two different database connections (local/remote)?
提问by Lucas Serafim
I looking for setup a multi-environment project using Laravel3 but I dont understand the command to set the environment.
我正在寻找使用 Laravel3 设置多环境项目,但我不明白设置环境的命令。
I see here: http://laravel.com/docs/artisan/commandsThe command is:
我在这里看到:http: //laravel.com/docs/artisan/commands命令是:
php artisan foo --env=local
I already used artisan and bob with success, what I can't undertand the foo, I try to change to my project name but always the same output: "Sorry, I can't find that task."
我已经成功地使用了 artisan 和 bob,我无法理解foo,我尝试更改为我的项目名称但始终输出相同的输出:“对不起,我找不到那个任务。”
If I try: php artisan --env=local
如果我尝试: php artisan --env=local
That will return: "You forgot to provide the task name."
这将返回:“您忘记提供任务名称。”
Anybody can help? Thanks for your time.
有人可以帮忙吗?谢谢你的时间。
[edit]With the answers now I can understand better and improve my question:
[编辑]现在有了答案,我可以更好地理解并改进我的问题:
I have a project with those folders created: http://d.pr/i/5nZSWith that in mind, I need to set my local env as development and production as production. So, I can do that with any variation of the command "php artisan --env=local" or I need to add on my public/.htaccess "SetEnv LARAVEL_ENV development"?
我有一个创建了这些文件夹的项目:http: //d.pr/i/5nZS 考虑到这一点,我需要将我的本地环境设置为开发,将生产设置为生产。那么,我可以使用“php artisan --env=local”命令的任何变体来做到这一点,还是需要在我的 public/.htaccess 中添加“SetEnv LARAVEL_ENV development”?
Thanks again.
再次感谢。
采纳答案by Lucas Serafim
Here, how I solved my question:
在这里,我如何解决我的问题:
First I didn't need the command php artisan migrate --env=local
, I just need set on my virtualhost: SetEnv LARAVEL_ENV development
.
首先我不需要命令php artisan migrate --env=local
,我只需要在我的虚拟主机上设置:SetEnv LARAVEL_ENV development
。
Second, as William Cahill-Manley say, I need to work on application/paths.php, the $environments. I've used it before but the wrong way. In my case, I solve with that:
其次,正如 William Cahill-Manley 所说,我需要处理 application/paths.php,即 $environments。我以前用过,但用错了方法。就我而言,我解决了这个问题:
$environments = array(
'development' => array('http://localhost/project*', '*project/*'),
'production' => array('http://project.com', 'http://*.project.com')
);
My problem was because my code before was like that:
我的问题是因为我之前的代码是这样的:
$environments = array(
'development' => array('http://localhost/project*', '*project*'),
'production' => array('http://project.com', 'http://*.project.com')
);
And because the second element of development array, in the production server always will be in development.
Thats because the url on development be http://project/
and on production be http://project.com/
or http://user.project.com/
并且因为开发数组的第二个元素,在生产服务器中总是会在开发中。那是因为开发http://project/
和生产的 url是http://project.com/
或http://user.project.com/
See, the projectwill force in all envonriments be development by the asterisk/wildcard.
看,该项目将强制在所有环境中使用星号/通配符进行开发。
回答by William Cahill-Manley
"Foo" is whatever command you want to run. E.g. for migrations:
“Foo”是您想要运行的任何命令。例如对于迁移:
php artisan migrate --env=local
Another thing you can do is add your computers hostname to this array
您可以做的另一件事是将您的计算机主机名添加到此数组中
For example, if my local computer name is 'Effinity.local' I could do
例如,如果我的本地计算机名称是“Effinity.local”,我可以这样做
$environments = array(
'local' => array('http://localhost*', 'Effinity.local'),
);
Then you do not need to specify the environment, just:
那么你不需要指定环境,只需:
php artisan migrate
Hope that helps.
希望有帮助。
回答by Jedism
I'd recommend setting up a name-based virtual host for your web application first:
我建议首先为您的 Web 应用程序设置一个基于名称的虚拟主机:
<VirtualHost *:80>
ServerAdmin postmaster@localhost
DocumentRoot "__PATH TO YOUR SERVER ROOT___/project/public/"
ServerName project.dev
ErrorLog "logs/project-error.log"
CustomLog "logs/project-access.log" combined
</VirtualHost>
Then add project.dev to your hosts file /private/etc/hosts
as follows:
然后将 project.dev 添加到您的主机文件中/private/etc/hosts
,如下所示:
127.0.0.1 project.dev
Don't forget to flush the DNS cache afterwards:
之后不要忘记刷新 DNS 缓存:
$ dscacheutil -flushcache
Then amend the $environments array found in [project root]/path.php
(the applications/path.php file you mentioned does not exist) back to what it was. The original *.dev wildcard will pick up the .dev at the end of the url you provided.
然后将在[project root]/path.php
(您提到的 applications/path.php 文件不存在)中找到的 $environments 数组修改回原来的样子。原始 *.dev 通配符将在您提供的 url 末尾获取 .dev。
$environments = array(
'local' => array('http://localhost*', '*.dev'),
);
Then create a directory within applications/config/
called local
, place within the new directory a file called application.php
. The configuration given in this file will override the configuration set by the corresponding configuration files and settings/values given in the parent of the local
directory you have created.
然后在applications/config/
called 中创建一个目录local
,在新目录中放置一个名为application.php
. 此文件中给出的配置将覆盖local
您创建的目录的父目录中给出的相应配置文件和设置/值所设置的配置。
回答by Knight
Foo is a task name , try to create this file in the task folder, there are some other task which is predefine for example migration etc.
Foo 是任务名称,尝试在任务文件夹中创建此文件,还有一些其他任务是预定义的,例如迁移等。
<?php
class foo_task {
public function run(){
echo 'this is foo';
}
}
?>
then when you run the command,it will run the code inside the run function.
然后当您运行该命令时,它将运行 run 函数中的代码。
php artisan foo --env=local