Laravel 5.1 没有使用正确的 .env 值
Laravel 5.1 not using correct .env values
我正在使用 Laravel 5.1.44,尽管我有一个具有不同值的 .env.testing
文件设置,以及 App::environment()
返回 'testing'
,Laravel 似乎仍然只使用主 .env
文件中的值。
还有什么我应该仔细检查以确保我没有搞砸或忘记设置的东西吗?
使用环境特定 .env
文件(例如 .env.testing
)的能力直到 Laravel 5.2.13 才可用。即便如此,它也只有在您不使用配置缓存时才有效。
最简单的选择是将所有测试特定环境变量添加到 phpunit.xml
文件中。正如你所提到的,你已经有了一些,添加你需要的任何额外的应该没有问题。
如果您真的想要 Laravel 5.1 中特定于环境的 .env
文件功能,您可以尝试以下操作。请注意,这是未经测试的,只是基于查看源代码的假设。
.env
文件加载到名为 DetectEnvironment
的 bootstrap class 中。这个想法是您创建自己的 bootstrap class 来修改 .env
文件的文件名,DetectEnvironment
将加载该文件,然后更新您的 Kernel
文件您的新 bootstrap class 被调用,并且在
DetectEnvironment
bootstrap每
因此,首先,为每个 class 创建一个新的 bootstrap(例如 app\Bootstrap\CustomEnvironment.php
):
<?php
namespace App\Bootstrap;
use Illuminate\Contracts\Foundation\Application;
class CustomEnvironment
{
/**
* Bootstrap the given application.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @return void
*/
public function bootstrap(Application $app)
{
// APP_ENV must already be set.
// We must know which environment file to look for.
if (! env('APP_ENV')) {
return;
}
// Build the file name to look for (e.g. .env.testing)
$file = $app->environmentFile().'.'.env('APP_ENV');
// If the file exists, set the App to load from that file.
// The actual loading will take place in DetectEnvironment
if (file_exists($app->environmentPath().'/'.$file)) {
$app->loadEnvironmentFrom($file);
}
}
}
现在您有一个 bootstrap class 来更新要加载的 .env
文件,您需要将它添加到您的 Kernel
|s 以确保它在 DetectEnvironment
bootstrap class.
之前调用
将以下 属性 添加到您的 app/Console/Kernel.php
文件中:
/**
* The bootstrap classes for the application.
*
* @var array
*/
protected $bootstrappers = [
'App\Bootstrap\CustomEnvironment',
'Illuminate\Foundation\Bootstrap\DetectEnvironment',
'Illuminate\Foundation\Bootstrap\LoadConfiguration',
'Illuminate\Foundation\Bootstrap\ConfigureLogging',
'Illuminate\Foundation\Bootstrap\HandleExceptions',
'Illuminate\Foundation\Bootstrap\RegisterFacades',
'Illuminate\Foundation\Bootstrap\SetRequestForConsole',
'Illuminate\Foundation\Bootstrap\RegisterProviders',
'Illuminate\Foundation\Bootstrap\BootProviders',
];
将以下 属性 添加到您的 app/Http/Kernel.php
文件中:
/**
* The bootstrap classes for the application.
*
* @var array
*/
protected $bootstrappers = [
'App\Bootstrap\CustomEnvironment',
'Illuminate\Foundation\Bootstrap\DetectEnvironment',
'Illuminate\Foundation\Bootstrap\LoadConfiguration',
'Illuminate\Foundation\Bootstrap\ConfigureLogging',
'Illuminate\Foundation\Bootstrap\HandleExceptions',
'Illuminate\Foundation\Bootstrap\RegisterFacades',
'Illuminate\Foundation\Bootstrap\RegisterProviders',
'Illuminate\Foundation\Bootstrap\BootProviders',
];
注意,乍一看它们可能看起来很相似,但是 Console
和 Http
内核之间的 bootstrappers 是不同的(Console
还有一个 bootstrap每)。不要只复制上面的其中一个并将其添加到两个文件中。
编辑
另一种更简单的方法是为 DetectEnvironment
bootstrapper 设置一个前置侦听器,并将文件设置为在事件闭包中加载。
因此,例如,在您的 bootstrap\app.php
文件中,在 return $app;
语句之前添加以下代码:
$app->beforeBootstrapping(
'Illuminate\Foundation\Bootstrap\DetectEnvironment',
function($app) {
// APP_ENV must already be set.
// We must know which environment file to look for.
if (! env('APP_ENV')) {
return;
}
// Build the file name to look for (e.g. .env.testing)
$file = $app->environmentFile().'.'.env('APP_ENV');
// If the file exists, set the App to load from that file.
// The actual loading will take place in DetectEnvironment
if (file_exists($app->environmentPath().'/'.$file)) {
$app->loadEnvironmentFrom($file);
}
}
);
使用beforeBootstrapping()
方法,第二个参数中的闭包将在第一个参数中的bootstrapper被调用之前立即执行。现在您不必担心内核或额外的 classes 或任何东西。
我正在使用 Laravel 5.1.44,尽管我有一个具有不同值的 .env.testing
文件设置,以及 App::environment()
返回 'testing'
,Laravel 似乎仍然只使用主 .env
文件中的值。
还有什么我应该仔细检查以确保我没有搞砸或忘记设置的东西吗?
使用环境特定 .env
文件(例如 .env.testing
)的能力直到 Laravel 5.2.13 才可用。即便如此,它也只有在您不使用配置缓存时才有效。
最简单的选择是将所有测试特定环境变量添加到 phpunit.xml
文件中。正如你所提到的,你已经有了一些,添加你需要的任何额外的应该没有问题。
如果您真的想要 Laravel 5.1 中特定于环境的 .env
文件功能,您可以尝试以下操作。请注意,这是未经测试的,只是基于查看源代码的假设。
.env
文件加载到名为 DetectEnvironment
的 bootstrap class 中。这个想法是您创建自己的 bootstrap class 来修改 .env
文件的文件名,DetectEnvironment
将加载该文件,然后更新您的 Kernel
文件您的新 bootstrap class 被调用,并且在
DetectEnvironment
bootstrap每
因此,首先,为每个 class 创建一个新的 bootstrap(例如 app\Bootstrap\CustomEnvironment.php
):
<?php
namespace App\Bootstrap;
use Illuminate\Contracts\Foundation\Application;
class CustomEnvironment
{
/**
* Bootstrap the given application.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @return void
*/
public function bootstrap(Application $app)
{
// APP_ENV must already be set.
// We must know which environment file to look for.
if (! env('APP_ENV')) {
return;
}
// Build the file name to look for (e.g. .env.testing)
$file = $app->environmentFile().'.'.env('APP_ENV');
// If the file exists, set the App to load from that file.
// The actual loading will take place in DetectEnvironment
if (file_exists($app->environmentPath().'/'.$file)) {
$app->loadEnvironmentFrom($file);
}
}
}
现在您有一个 bootstrap class 来更新要加载的 .env
文件,您需要将它添加到您的 Kernel
|s 以确保它在 DetectEnvironment
bootstrap class.
将以下 属性 添加到您的 app/Console/Kernel.php
文件中:
/**
* The bootstrap classes for the application.
*
* @var array
*/
protected $bootstrappers = [
'App\Bootstrap\CustomEnvironment',
'Illuminate\Foundation\Bootstrap\DetectEnvironment',
'Illuminate\Foundation\Bootstrap\LoadConfiguration',
'Illuminate\Foundation\Bootstrap\ConfigureLogging',
'Illuminate\Foundation\Bootstrap\HandleExceptions',
'Illuminate\Foundation\Bootstrap\RegisterFacades',
'Illuminate\Foundation\Bootstrap\SetRequestForConsole',
'Illuminate\Foundation\Bootstrap\RegisterProviders',
'Illuminate\Foundation\Bootstrap\BootProviders',
];
将以下 属性 添加到您的 app/Http/Kernel.php
文件中:
/**
* The bootstrap classes for the application.
*
* @var array
*/
protected $bootstrappers = [
'App\Bootstrap\CustomEnvironment',
'Illuminate\Foundation\Bootstrap\DetectEnvironment',
'Illuminate\Foundation\Bootstrap\LoadConfiguration',
'Illuminate\Foundation\Bootstrap\ConfigureLogging',
'Illuminate\Foundation\Bootstrap\HandleExceptions',
'Illuminate\Foundation\Bootstrap\RegisterFacades',
'Illuminate\Foundation\Bootstrap\RegisterProviders',
'Illuminate\Foundation\Bootstrap\BootProviders',
];
注意,乍一看它们可能看起来很相似,但是 Console
和 Http
内核之间的 bootstrappers 是不同的(Console
还有一个 bootstrap每)。不要只复制上面的其中一个并将其添加到两个文件中。
编辑
另一种更简单的方法是为 DetectEnvironment
bootstrapper 设置一个前置侦听器,并将文件设置为在事件闭包中加载。
因此,例如,在您的 bootstrap\app.php
文件中,在 return $app;
语句之前添加以下代码:
$app->beforeBootstrapping(
'Illuminate\Foundation\Bootstrap\DetectEnvironment',
function($app) {
// APP_ENV must already be set.
// We must know which environment file to look for.
if (! env('APP_ENV')) {
return;
}
// Build the file name to look for (e.g. .env.testing)
$file = $app->environmentFile().'.'.env('APP_ENV');
// If the file exists, set the App to load from that file.
// The actual loading will take place in DetectEnvironment
if (file_exists($app->environmentPath().'/'.$file)) {
$app->loadEnvironmentFrom($file);
}
}
);
使用beforeBootstrapping()
方法,第二个参数中的闭包将在第一个参数中的bootstrapper被调用之前立即执行。现在您不必担心内核或额外的 classes 或任何东西。