Laravel/Lumen 5.3.3:覆盖迁移中的 env 值
Laravel/Lumen 5.3.3: override env values in migrations
在 laravel 文档中我清楚地看到:
https://laravel.com/docs/5.3/configuration#environment-configuration
You may also create a .env.testing file. This file will override
values from the .env file when running PHPUnit tests or executing
Artisan commands with the --env=testing option.
所以我想通过添加 .env.migration
文件并覆盖 mysql 用户凭据,我可以 运行 我与另一个用户的迁移:
php artisan migrate --env=migration
好吧,它不仅不会覆盖以前定义的值,甚至不会向环境添加新值!事实上,artisan 命令根本不加载文件。我把文件名改成了testing,没有用。我更新了我的作曲家,仍然没有结果。
我有一个简单的问题。 使用另一个数据库凭据处理 laravel 迁移的最佳方法是什么?
如果我知道 lumen 包中的迁移脚本在哪里,也很受欢迎,这样我就可以开始研究脚本了。
此问题已在 5.3.11 this PR 中得到修复。如果您至少升级到该版本,artisan 命令将在加载 .env
文件时遵守 --env
设置。
如果您使用的是 Lumen,则需要更新 bootstrap/app.php
文件。
靠近文件顶部的是这段代码:
try {
(new Dotenv\Dotenv(__DIR__.'/../'))->load();
} catch (Dotenv\Exception\InvalidPathException $e) {
//
}
应将其替换为类似以下内容:
$suffix = '';
if (php_sapi_name() == 'cli') {
$input = new Symfony\Component\Console\Input\ArgvInput;
if ($input->hasParameterOption('--env')) {
$suffix = '.'.$input->getParameterOption('--env');
}
} elseif (env('APP_ENV')) {
$suffix = '.'.env('APP_ENV');
}
$env = '.env';
if ($suffix && file_exists(__DIR__.'/../'.$env.$suffix)) {
$env .= $suffix;
}
try {
(new Dotenv\Dotenv(__DIR__.'/../', $env))->load();
} catch (Dotenv\Exception\InvalidPathException $e) {
//
}
注意:未经测试,但这个想法应该可行。
这是我用来在运行时更新特定 .env 值的方法(即使配置已缓存)。我会把安全警告放在那里,应该严格保护对这样的方法的调用,并且应该正确清理用户输入。
private function setEnvironmentValue($environmentName, $configKey, $newValue) {
file_put_contents(App::environmentFilePath(), str_replace(
$environmentName . '=' . Config::get($configKey),
$environmentName . '=' . $newValue,
file_get_contents(App::environmentFilePath())
));
Config::set($configKey, $newValue);
// Reload the cached config
if (file_exists(App::getCachedConfigPath())) {
Artisan::call("config:cache");
}
}
它的一个使用示例是;
$this->setEnvironmentValue('APP_LOG_LEVEL', 'app.log_level', 'debug');
$environmentName
是环境文件中的键(例如.. APP_LOG_LEVEL)
$configKey
是用于在运行时访问配置的密钥(示例.. app.log_level (tinker config('app.log_level')
).
$newValue
当然是你希望坚持的新价值。
试试这个:
try {
// < L5.8
// (\Dotenv\Dotenv(dirname(__DIR__,1), '.env')->overload();
// >= L5.8
( \Dotenv\Dotenv::createImmutable( dirname(__DIR__,1).'/.env' ) )->load();
} catch ( \Dotenv\Dotenv\Exception\InvalidPathException $e ) {
//
}
在 laravel 文档中我清楚地看到:
https://laravel.com/docs/5.3/configuration#environment-configuration
You may also create a .env.testing file. This file will override values from the .env file when running PHPUnit tests or executing Artisan commands with the --env=testing option.
所以我想通过添加 .env.migration
文件并覆盖 mysql 用户凭据,我可以 运行 我与另一个用户的迁移:
php artisan migrate --env=migration
好吧,它不仅不会覆盖以前定义的值,甚至不会向环境添加新值!事实上,artisan 命令根本不加载文件。我把文件名改成了testing,没有用。我更新了我的作曲家,仍然没有结果。
我有一个简单的问题。 使用另一个数据库凭据处理 laravel 迁移的最佳方法是什么?
如果我知道 lumen 包中的迁移脚本在哪里,也很受欢迎,这样我就可以开始研究脚本了。
此问题已在 5.3.11 this PR 中得到修复。如果您至少升级到该版本,artisan 命令将在加载 .env
文件时遵守 --env
设置。
如果您使用的是 Lumen,则需要更新 bootstrap/app.php
文件。
靠近文件顶部的是这段代码:
try {
(new Dotenv\Dotenv(__DIR__.'/../'))->load();
} catch (Dotenv\Exception\InvalidPathException $e) {
//
}
应将其替换为类似以下内容:
$suffix = '';
if (php_sapi_name() == 'cli') {
$input = new Symfony\Component\Console\Input\ArgvInput;
if ($input->hasParameterOption('--env')) {
$suffix = '.'.$input->getParameterOption('--env');
}
} elseif (env('APP_ENV')) {
$suffix = '.'.env('APP_ENV');
}
$env = '.env';
if ($suffix && file_exists(__DIR__.'/../'.$env.$suffix)) {
$env .= $suffix;
}
try {
(new Dotenv\Dotenv(__DIR__.'/../', $env))->load();
} catch (Dotenv\Exception\InvalidPathException $e) {
//
}
注意:未经测试,但这个想法应该可行。
这是我用来在运行时更新特定 .env 值的方法(即使配置已缓存)。我会把安全警告放在那里,应该严格保护对这样的方法的调用,并且应该正确清理用户输入。
private function setEnvironmentValue($environmentName, $configKey, $newValue) {
file_put_contents(App::environmentFilePath(), str_replace(
$environmentName . '=' . Config::get($configKey),
$environmentName . '=' . $newValue,
file_get_contents(App::environmentFilePath())
));
Config::set($configKey, $newValue);
// Reload the cached config
if (file_exists(App::getCachedConfigPath())) {
Artisan::call("config:cache");
}
}
它的一个使用示例是;
$this->setEnvironmentValue('APP_LOG_LEVEL', 'app.log_level', 'debug');
$environmentName
是环境文件中的键(例如.. APP_LOG_LEVEL)
$configKey
是用于在运行时访问配置的密钥(示例.. app.log_level (tinker config('app.log_level')
).
$newValue
当然是你希望坚持的新价值。
试试这个:
try {
// < L5.8
// (\Dotenv\Dotenv(dirname(__DIR__,1), '.env')->overload();
// >= L5.8
( \Dotenv\Dotenv::createImmutable( dirname(__DIR__,1).'/.env' ) )->load();
} catch ( \Dotenv\Dotenv\Exception\InvalidPathException $e ) {
//
}