The. env file can customize any other valid environment variables and can be call env () or $_server or $_env To get the variable. So how does env () load into these variables? In Lumen's vendor/laravel/lumen-framework/src/helpers.php, we can see that the ENV function is defined like this:
if (function_exists (' env ')) {/** * Gets the value of an environment variable. S Upports Boolean, empty, and null. * * @param string $key * @param mixed $default * @return mixed */function env ($key, $default = null) {$value = getenv ($key), if ($value = = = False) {return value ($default); }switch (Strtolower ($value)) {case ' true ': Case ' (true) ': Return True;case ' false ': Case ' (false) ': Return false;case ' Empty ': Case ' (empty) ': return ', case ' null ': Case ' (null) ': return; }if (Str::startswith ($value, ' "') && str::endswith ($value, '" ')) {return substr ($value, 1,-1); }return $value; }}
Visible, the ENV function calls getenv () to read the environment variable. We also know that getenv () is a function of PHP native to provide readable $_server or $_env Global Variables api,.env file environment variables can be obtained by getenv ()? Vlucas/phpdotenv is this behind the hero, in Lumen or Laravel vendor can find her, if you want to download her separately, go here:.
In the vlucas/phpdotenv/src/loader.php file, we can see that the. env is loaded into an array, and each row is treated as a setter and called the Setenvironmentvariable () method:
/** * Load '. env ' file in given directory. * * @return array */public function load () {$this->ensurefileisreadable (); $filePath = $this FilePath; $lines = $this->readlinesfromfile ($filePath), foreach ($lines as $line) {if (! $this->iscomment ($line) && $this->lookslikesetter ($line) {$this->setenvironmentvariable ($line);} } return $lines; } /** * Read lines from the file, auto detecting line endings. * * @param string $filePath * * @return array */protected function ReadLinesFromFile ($filePath) {//Read file into a array of lines with auto-detected line endings$autodetect = Ini_get (' auto_detect_line_endings ' ); Ini_set (' auto_detect_line_endings ', ' 1 '); $lines = File ($filePath, File_ignore_new_lines | File_skip_empty_lines) ini_set (' auto_detect_line_endings ', $autodetect); return $lines; }
loader::setenvironmentvariable ( $name , $value = null ) is defined as follows:
/** * Set an environment variable. * * This is the done using: *-putenv, *-$_env, *-$_server. * * The environment variable value is stripped of single and double quotes. * * @param string $name * @param string|null $value * * @return void */public function Setenviron Mentvariable ($name, $value = null) {list ($name, $value) = $this->normaliseenvironmentvariable ($name, $value);//Don ' T overwrite existing environment variables if we ' re immutable//Ruby ' s dotenv does this with ' Env[key ' | | = Value '. if ($this->immutable && $this->getenvironmentvariable ($name)!== null) {return; }//If PHP is running as a Apache module and an existing//Apache environment variable exists, overwrite itif (fu Nction_exists (' apache_getenv ') && function_exists (' apache_setenv ') && apache_getenv ($name)) {A Pache_setenv ($name, $value); } if (Function_exists (' putenv ')) { Putenv ("$name = $value"); } $_env[$name] = $value; $_server[$name] = $value;}
PHP dotenv What is she?
Loads environment variables from. Env to Getenv (), $_env and $_server automagically. This is a PHP version of the original Ruby dotenv.
Why. Env?
You should never store sensitive credentials in your code. Storing configuration in the environment are one of the tenets of a twelve-factor app. Anything that's likely to change be Tween deployment Environments–such as database credentials or credentials for 3rd party services–should be extracted F Rom the code into environment variables. Basically, a. env file is the easy-to-load custom configuration variables that your application needs without have to Modify. htaccess files or Apache/nginx virtual hosts. This means won ' t has to edit any files outside the project, and all the environment variables is always set no matte R How do you run your project-apache, Nginx, CLI, and even PHP 5.4 ' s built-in webserver. It's the easier than all the other ways you know's to set environment variables, and your ' re going to love it. NO editing virtual hosts in Apache or Nginx. NO adding Php_value flags to. htaccess files. Easy portability and sharing of required ENV values. COMPATIBLE WitH PHP ' s built-in Web server and CLI runner