简体   繁体   English

更改Laravel 4中的默认环境

[英]Changing default environment in Laravel 4

In Laravel 4 the default configuration environment is 'production'. 在Laravel 4中,默认配置环境是“生产”。 This means that if you run an artisan command without the --env option, it assumes the production configuration. 这意味着如果您运行没有--env选项的artisan命令,它将采用生产配置。 This can be seen in \\Illuminate\\Foundation\\Application::detectWebEnvironment() which is called by detectConsoleEnvironment() when no --env option is set. 这可以在\\Illuminate\\Foundation\\Application::detectWebEnvironment()看到,当没有设置--env选项时,由detectConsoleEnvironment()调用。

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. 很容易忘记--env选项,比如无意中在生产数据库上运行迁移。 (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. (是的,发生了这件事,但幸运的是,这是一个小小的改变。)我接近将生产环境配置重命名为“实际生产”,但似乎应该有一个更优雅的解决方案。

TL;DR TL; DR

How can I change the default environment in Laravel 4 such that artisan commands do not run on production by default? 如何更改Laravel 4中的默认环境,以便默认情况下artisan命令不会在生产中运行?

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 == false will work. 我测试了似乎任何东西== false都可行。 I also verified this does not interfere with the detection of the production or testing environments. 我还证实了这不会干扰生产或测试环境的检测。

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'];
});

You can try modifying app/start.php file to add second parameter on desired environment as TRUE ie to enable local environment it looks like 您可以尝试修改app / start.php文件,将所需环境中的第二个参数添加为TRUE,即启用它看起来的本地环境

$env = $app->detectEnvironment(array(

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

));

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 . 无需将开发机器阵列硬编码到git commit文件start.php
  2. Fallback to server environmental variables in production. 在生产中回退到服务器环境变量。
  3. Easy persist local development environment by modifying the environment.php file. 通过修改environment.php文件,可以轻松保留本地开发环境。
$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

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的更改日志在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM