更改 Laravel 4 中的默认环境

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

Changing default environment in Laravel 4

phplaravellaravel-4

提问by Collin James

In Laravel 4 the default configuration environment is 'production'. This means that if you run an artisan command without the --envoption, it assumes the production configuration. This can be seen in \Illuminate\Foundation\Application::detectWebEnvironment()which is called by detectConsoleEnvironment()when no --envoption is set.

在 Laravel 4 中,默认配置环境是“生产”。这意味着如果您在没有--env选项的情况下运行 artisan 命令,它将采用生产配置。这可以在没有设置选项时\Illuminate\Foundation\Application::detectWebEnvironment()调用 which中看到。detectConsoleEnvironment()--env

This behavior has become a risk with my development environment. It's really easy to forget the --env option and, say, unintentionally run a migration on your production database. (Yes, that happened but thankfully it was a minor change.) I'm close to just renaming my production environment config to 'real-production' but it seems like there should be a more elegant solution.

这种行为已成为我的开发环境的风险。忘记 --env 选项真的很容易,比如说,无意中在您的生产数据库上运行迁移。(是的,这发生了,但幸运的是这是一个小改动。)我即将将我的生产环境配置重命名为“真实生产”,但似乎应该有一个更优雅的解决方案。

TL;DR

TL; 博士

How can I change the default environment in Laravel 4 such that artisan commands do not run on production by default?

如何更改 Laravel 4 中的默认环境,以便默认情况下不会在生产环境中运行 artisan 命令?

采纳答案by Collin James

Thanks Antonio for prompting me to reconsider domain detection.

感谢 Antonio 促使我重新考虑域检测。

$env = $app->detectEnvironment(array(
(
    // Empty string is to set development as the default environment for 
    // artisan commands.
    'development' => array('dev.foo.com', ''),
    'test' => array('test.foo.com'),
    'production' => array('www.foo.com', 'foo.com'),
));

Adding '' as a development domain effectively sets development as the default environment for artisan commands, presumably because the domain name is blank when the application is invoked from the command line. I tested and it seems anything == falsewill work. I also verified this does not interfere with the detection of the production or testing environments.

添加 '' 作为开发域有效地将开发设置为 artisan 命令的默认环境,大概是因为从命令行调用应用程序时域名为空。我测试过,似乎什么== false都行。我还验证了这不会干扰生产或测试环境的检测。

回答by Antonio Carlos Ribeiro

In bootstrap/start.php you can set the environment:

在 bootstrap/start.php 中你可以设置环境:

$env = $app->detectEnvironment(function()
{

    return 'development';

});

But you can do many things like:

但是你可以做很多事情,比如:

$env = $app->detectEnvironment(array(

    'local' => array('your-machine-name'),

));

And

$env = $app->detectEnvironment(function()
{
    return $_SERVER['MY_LARAVEL_ENV'];
});

回答by rabin

You can try modifying app/start.php file to add second parameter on desired environment as TRUEi.e. to enable local environment it looks like

您可以尝试修改 app/start.php 文件以将所需环境的第二个参数添加为TRUE,即启用它看起来像的本地环境

$env = $app->detectEnvironment(array(

    'local' => array('homestead',true),

));

回答by randomor

One of the most elegant solution that I've found is from this blog post: http://stevegrunwell.com/blog/laravel-application-environment/

我发现的最优雅的解决方案之一来自这篇博客文章:http: //stevegrunwell.com/blog/laravel-application-environment/

The advantages:

优点:

  1. No need to hardcode an array of development machines into a git committed file start.php.
  2. Fallback to server environmental variables in production.
  3. Easy persist local development environment by modifying the environment.phpfile.
  1. 无需将一组开发机器硬编码到 git 提交文件中start.php
  2. 回退到生产中的服务器环境变量。
  3. 通过修改environment.php文件轻松持久化本地开发环境。

回答by jianfeng

$env = $app->detectEnvironment(array(

      'staging' => array('baichebao_test'),
      'local' => array('*.local', '*'),
 ));

like my example, put your default environment in the last item of array, and add "*" to it's manager hostname. and it works in laravel 4.X

就像我的例子一样,把你的默认环境放在数组的最后一项,并在它的管理器主机名中添加“*”。它适用于 laravel 4.X

回答by Laurence

In Laravel 4.2 you will not be able to do destructive artisan migrations without being prompted:

在 Laravel 4.2 中,您将无法在没有提示的情况下进行破坏性的工匠迁移:

Destructive migration operations now require confirmation or --force when being run in production.

在生产中运行时,破坏性迁移操作现在需要确认或 --force。

Change log for 4.2 is here

4.2 的变更日志在这里