Chi*_*lan 17 php environment-variables laravel
我有一个自定义CMS,我在Laravel中从头开始编写,并希望env在用户设置后从控制器设置值,即数据库详细信息,邮件程序详细信息,常规配置等,并希望为用户提供在运行时更改它们的灵活性我正在制作的GUI.
所以我的问题是如何.env从控制器中将我从用户收到的值写入文件.
并且.env随时随地构建文件是一个好主意还是有其他方法吗?
提前致谢.
Ale*_*nin 34
由于Laravel使用配置文件来访问和存储.env数据,因此您可以使用以下config()方法动态设置此数据:
config(['database.connections.mysql.host' => '127.0.0.1']);
Run Code Online (Sandbox Code Playgroud)
要使用此数据config():
config('database.connections.mysql.host')
Run Code Online (Sandbox Code Playgroud)
要在运行时设置配置值,请将数组传递给
config帮助程序
https://laravel.com/docs/5.3/configuration#accessing-configuration-values
小智 21
小心!并非所有laravel .env中的变量都存储在配置环境中.要覆盖真正的.env内容,请使用:
putenv("CUSTOM_VARIABLE =英雄");
像往常一样阅读,env('CUSTOM_VARIABLE')或env('CUSTOM_VARIABLE','devault')
ves*_*ght 13
根据乔希的回答.我需要一种方法来替换.env文件中的键值.
但与josh的回答不同,我不想依赖于知道当前值或配置文件中可访问的当前值.
因为我的目标是替换Laravel Envoy使用的值,它根本不使用配置文件,而是直接使用该.env文件.
这是我的看法:
public function setEnvironmentValue($envKey, $envValue)
{
$envFile = app()->environmentFilePath();
$str = file_get_contents($envFile);
$oldValue = strtok($str, "{$envKey}=");
$str = str_replace("{$envKey}={$oldValue}", "{$envKey}={$envValue}\n", $str);
$fp = fopen($envFile, 'w');
fwrite($fp, $str);
fclose($fp);
}
Run Code Online (Sandbox Code Playgroud)
用法:
$this->setEnvironmentValue('DEPLOY_SERVER', 'forge@122.11.244.10');
Run Code Online (Sandbox Code Playgroud)
小智 8
基于totymedli的答案。
如果需要一次更改多个环境变量值,则可以传递一个数组(键->值)。将添加以前不存在的任何密钥,并返回布尔值,以便您可以测试成功。
public function setEnvironmentValue(array $values)
{
$envFile = app()->environmentFilePath();
$str = file_get_contents($envFile);
if (count($values) > 0) {
foreach ($values as $envKey => $envValue) {
$str .= "\n"; // In case the searched variable is in the last line without \n
$keyPosition = strpos($str, "{$envKey}=");
$endOfLinePosition = strpos($str, "\n", $keyPosition);
$oldLine = substr($str, $keyPosition, $endOfLinePosition - $keyPosition);
// If key does not exist, add it
if (!$keyPosition || !$endOfLinePosition || !$oldLine) {
$str .= "{$envKey}={$envValue}\n";
} else {
$str = str_replace($oldLine, "{$envKey}={$envValue}", $str);
}
}
}
$str = substr($str, 0, -1);
if (!file_put_contents($envFile, $str)) return false;
return true;
}
Run Code Online (Sandbox Code Playgroud)
根据vesperknight 的回答,我创建了一个不使用strtok或的解决方案env()。
private function setEnvironmentValue($envKey, $envValue)
{
$envFile = app()->environmentFilePath();
$str = file_get_contents($envFile);
$str .= "\n"; // In case the searched variable is in the last line without \n
$keyPosition = strpos($str, "{$envKey}=");
$endOfLinePosition = strpos($str, PHP_EOL, $keyPosition);
$oldLine = substr($str, $keyPosition, $endOfLinePosition - $keyPosition);
$str = str_replace($oldLine, "{$envKey}={$envValue}", $str);
$str = substr($str, 0, -1);
$fp = fopen($envFile, 'w');
fwrite($fp, $str);
fclose($fp);
}
Run Code Online (Sandbox Code Playgroud)
这不使用strtok它可能对某些人不起作用,或者env()不适.env用于评估并插入嵌入变量的双引号变量
KEY="Something with spaces or variables ${KEY2}"
Run Code Online (Sandbox Code Playgroud)
更简化:
public function putPermanentEnv($key, $value)
{
$path = app()->environmentFilePath();
$escaped = preg_quote('='.env($key), '/');
file_put_contents($path, preg_replace(
"/^{$key}{$escaped}/m",
"{$key}={$value}",
file_get_contents($path)
));
}
Run Code Online (Sandbox Code Playgroud)
或作为帮助者:
if ( ! function_exists('put_permanent_env'))
{
function put_permanent_env($key, $value)
{
$path = app()->environmentFilePath();
$escaped = preg_quote('='.env($key), '/');
file_put_contents($path, preg_replace(
"/^{$key}{$escaped}/m",
"{$key}={$value}",
file_get_contents($path)
));
}
}
Run Code Online (Sandbox Code Playgroud)
小智 7
#更简单的方法来覆盖 .env 你可以这样做
$_ENV['key'] = 'value';
Run Code Online (Sandbox Code Playgroud)
小智 5
如果您不需要将更改保存到 .env 文件,您可以简单地使用以下代码:
\Illuminate\Support\Env::getRepository()->set('APP_NAME','New app name');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
36351 次 |
| 最近记录: |