Laravel如何解析.env文件?

时间:2022-10-18 17:33:41

I would like to mimic the way a Laravel application has it's environment variables set via a .env file.

我想模仿Laravel应用程序通过.env文件设置环境变量的方式。

APP_ENV=local
DB_DATABASE=fruits
DB_USERNAME=fruituser
DB_PASSWORD=secretpassword

So it can then set default fallbacks in config.php like this:

所以它可以在config.php中设置默认回退,如下所示:

return [
    'env' => env('APP_ENV', 'production'),
];

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.

但是,我无法深入挖掘框架代码,找到它在.env中解析文本的位,并将其转换为适当的PHP变量。

I have found the definition of the env() helper function in vendor\laravel\framework\src\Illuminate\Foundation\helpers.php:

我在vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ helpers.php中找到了env()辅助函数的定义:

function env($key, $default = null)
{
    $value = getenv($key);

    if ($value === false) {
        return value($default);

    }
...

...but that calls another global helper function called getenv() and that is where the trail goes cold.

...但是它调用另一个名为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.

我怀疑我们现在可能已经达到了Symfony水平,但是我找不到getenv()的定义,我们将非常感谢您的帮助和指导。

1 个解决方案

#1


3  

Laravel Uses this libarary for it

Laravel使用这个图书馆

https://github.com/vlucas/phpdotenv

https://github.com/vlucas/phpdotenv

#1


3  

Laravel Uses this libarary for it

Laravel使用这个图书馆

https://github.com/vlucas/phpdotenv

https://github.com/vlucas/phpdotenv