Loading of environment variables in LARAVEL5

Source: Internet
Author: User

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\DetectEnvironmentStart 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.