Laravel如何解析.env文件? [英] How does Laravel parse the .env file?
问题描述
我想模仿Laravel应用程序通过 .env
文件设置环境变量的方式.
I would like to mimic the way a Laravel application has it's environment variables set via a .env
file.
APP_ENV=local
DB_DATABASE=fruits
DB_USERNAME=fruituser
DB_PASSWORD=secretpassword
因此它可以像这样在 config.php
中设置默认后备:
So it can then set default fallbacks in config.php
like this:
return [
'env' => env('APP_ENV', 'production'),
];
但是,我在浏览框架代码时难以找到解析 .env
中文本的位,并将其转换为适当的PHP变量.
However I am having trouble digging through the framework code to find the bit where it parses the text in .env
and turns it into proper PHP variables.
我在 vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ helpers.php
中找到了 env()
帮助器函数的定义:
I have found the definition of the env()
helper function in vendor\laravel\framework\src\Illuminate\Foundation\helpers.php
:
function env($key, $default = null)
{
$value = getenv($key);
if ($value === false) {
return value($default);
}
...
...但是调用另一个名为 getenv()
的全局帮助器函数,这就是线索变冷的地方.
...but that calls another global helper function called getenv()
and that is where the trail goes cold.
我怀疑我们现在可能已经降到Symfony级别,但是可惜我找不到 getenv()
的定义,您的帮助和指导将不胜感激.
I suspect we might be down to Symfony level now but alas I cannot find the definition of getenv()
and your help and guidance would be greatly appreciated.
推荐答案
Laravel为此使用此库
Laravel Uses this libarary for it
https://github.com/vlucas/phpdotenv
这篇关于Laravel如何解析.env文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!