问题
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