Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Laravel parse the .env file?

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.

like image 701
Martin Joiner Avatar asked Sep 04 '17 11:09

Martin Joiner


People also ask

How does the .env file work?

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.

For what do the .env is used in Laravel?

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.

Where is the .env file in Laravel?

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').


1 Answers

Laravel Uses this libarary for it

https://github.com/vlucas/phpdotenv

like image 168
Manish Champaneri Avatar answered Oct 16 '22 03:10

Manish Champaneri