更改Laravel 4中的默认环境

时间:2022-03-04 22:46:20

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

在Laravel 4中,默认配置环境是“生产”。这意味着如果您运行没有--env选项的artisan命令,它将采用生产配置。这可以在\ 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. (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

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

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

6 个解决方案

#1


12  

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. I also verified this does not interfere with the detection of the production or testing environments.

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

#2


10  

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

#3


3  

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

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

$env = $app->detectEnvironment(array(

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

));

#4


2  

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. 无需将开发机器阵列硬编码到git commit文件start.php中。

  3. Fallback to server environmental variables in production.
  4. 在生产中回退到服务器环境变量。

  5. Easy persist local development environment by modifying the environment.php file.
  6. 通过修改environment.php文件,可以轻松保留本地开发环境。

#5


1  

$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

#6


0  

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

#1


12  

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. I also verified this does not interfere with the detection of the production or testing environments.

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

#2


10  

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

#3


3  

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

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

$env = $app->detectEnvironment(array(

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

));

#4


2  

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. 无需将开发机器阵列硬编码到git commit文件start.php中。

  3. Fallback to server environmental variables in production.
  4. 在生产中回退到服务器环境变量。

  5. Easy persist local development environment by modifying the environment.php file.
  6. 通过修改environment.php文件,可以轻松保留本地开发环境。

#5


1  

$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

#6


0  

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