How does Laravel parse the .env file?

我们两清 提交于 2021-01-02 05:18:24

问题


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

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

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.

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);

    }
...

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

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.


回答1:


Laravel Uses this libarary for it

https://github.com/vlucas/phpdotenv



来源:https://stackoverflow.com/questions/46036541/how-does-laravel-parse-the-env-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!