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.
env. example file documents the application's necessary variables and can be committed to version control. This serves as a helpful reference and speeds up the onboarding process for new team members by reducing the amount of time spent digging through the coding to figure out what needs to be set up.
Laravel's default .env file contains some common configuration values that may differ based on whether your application is running locally or on a production web server. These values are then retrieved from various Laravel configuration files within the config directory using Laravel's env function.
In your main Laravel folder you should have . env file which contains various settings, one row – one KEY=VALUE pair. And then, within your Laravel project code you can get those environment variables with function env('KEY').
Laravel Uses this libarary for it
https://github.com/vlucas/phpdotenv
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With