Loading bootstrap of environment variables in LARAVEL5
The first startup item you can find in kernel is about env.
protected $bootstrappers = [ ‘Illuminate\Foundation\Bootstrap\DetectEnvironment‘, ‘Illuminate\Foundation\Bootstrap\LoadConfiguration‘, ‘Illuminate\Foundation\Bootstrap\ConfigureLogging‘, ‘Illuminate\Foundation\Bootstrap\HandleExceptions‘, ‘Illuminate\Foundation\Bootstrap\RegisterFacades‘, ‘Illuminate\Foundation\Bootstrap\RegisterProviders‘, ‘Illuminate\Foundation\Bootstrap\BootProviders‘,];
Illuminate\Foundation\Bootstrap\DetectEnvironment
Start in this class, go to find, and you will find
try{ Dotenv::load($app->basePath(), $app->environmentFile());}catch (InvalidArgumentException $e){ //}$app->detectEnvironment(function(){ return env(‘APP_ENV‘, ‘production‘);});
The specific method is a static load method for Dotenv, and if the read fails, it is set to ' production ' by default
Here you can see that the load passed in the two parameters is to find the location of the read file, $app->environmentFile()
the return is the filename, is $app->environmentfile, the default is .env
. There is one method in $app:
public function loadEnvironmentFrom($file){ $this->environmentFile = $file; return $this;}
Use this method to modify the name of the env file that is loaded.
Parse env file
The Dotenv::load method first determines the file is_readable and Is_file, otherwise throws an exception. Ini_get, Ini_set set the auto_detect_line_endings to 1, and then set the original value.
file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
Read each line of the file, and then the foreach traversal, and then set the environment variable. It can be found that nestedvariable is supported here, that is, the set of variables can be used $ (var_exists) in reading its value, similar to the shell.
The set variable exists in three positions, PUTEVN (), $_env, $_server, and will determine if the key is duplicated if it is repeated.
Read time is also from these three locations, getenv (),$_env, $_server.
Several ways to do Env in the app:
public function environmentFile()
Default return '. Env '
public function environment()
Returns the value of the current env without parameters, or, if the parameter (array) is present, returns true if the current environment name is included.
public function isLocal()
$app [' env '] equals ' local '
public function afterLoadingEnvironment(Closure $callback)
public function detectEnvironment(Closure $callback)
Loading of environment variables in LARAVEL5