A simple way to set the environment variable configuration. env

Source: Internet
Author: User
what is. Env

The. env file is located under the project root as the Global environment configuration file.

Environment variables are loaded through the. env file and automatically invoked via Getenv (), $_env, and $_server.
This is a PHP version of Ruby dotenv. Sample

Run environment name
app_env=local
//debug mode, development phase enabled, online status disabled.
app_debug=true
//sensitive information encryption key, can be regenerated using PHP artisan key:generate.
app_key=
//project root directory
app_url=http://localhost

//cache driver, default use file as cache.
cache_driver=file
//Reply-driven, default to use file storage session.
session_driver=file
//queue driver, using sync mode by default.
queue_driver=sync

//Redis high-performance Key-value database, using memory storage for data persistence.
redis_host=127.0.0.1
Redis_password=null
redis_port=6379

//Database configuration Items
db_host=127.0.0.1//database host name
db_port=3306//database port
db_database=homestead//Database name
db_username=homestead//Database login account
db_password=secret//Database login Password
AdvantagesAdd. env to Git's. gitignore file to avoid sensitive information disclosure in the. env file you can set environment variables without modifying the Apache/nginx configuration file. Add Php_value value to the. htaccess to facilitate porting and sharing of Env environment variable value compatible server built-in server and CLI runtime using composer installation
Curl-s Http://getcomposer.org/installer | PHP
php composer.phar require vlucas/phpdotenv
How to use

The. env file is not usually included in version control, and it may contain sensitive API keys or passwords. All projects that require an environment variable definition (insensitive definition) need to create a. env.example file that contains their own defined environment variables or federated development of the contained environment variables. Project partners can independently replicate. Env.example and rename to. env, and modify to the correct local environment configuration to store the password key or provide them with the necessary values. In this use method. env files should be added to the. gitignore file and will never be checked in/checked out by the project's collaborators. This approach ensures that there are no sensitive API keys or passwords that appear in version control to reduce security risks. And the configuration in the development environment will never inform the partner developer.

Add a. env file configured to the root directory to ensure that the. env file is added to. Gitignore and will not be checked in to CVS

S3_bucket=dotenv
Secret_key=souper_seekret_key

Now create a. env.example file and check it in to your project. Here the configuration and environment variables you need to set can be left blank or set up some irrelevant data. This method tells people that the data is necessary, but does not provide data in the real environment.

S3_bucket=devbucket
secret_key=abc123

You can use the following line of code to load the. env file in the application:

Dotenv::load (__dir__);

All defined variables can be accessed through the Getenv method, and can also be accessed using Hyper-global variables $_env and $_server.

$s 3_bucket = getenv (' S3_bucket ');
$s 3_bucket = $_env[' S3_bucket '];
$s 3_bucket = $_server[' S3_bucket '];

You can also use the framework's Request class to access these variables (if you use frames)

$s 3_bucket = $request->env (' S3_bucket ');
$s 3_bucket = $request->getenv (' S3_bucket ');
$s 3_bucket = $request->server->get (' S3_bucket ');
Embedded Variables

Embedding an environment variable in a variable is OK, which is useful for reducing duplication.

Use {$ ...} to wrap environment variable e.g.

Base_dir=/var/webroot/project-root
cache_dir={$BASE _dir}/cache
tmp_dir={$BASE _dir}/tmp
not to change

By default, dotenv that the environment variable is unchanged. This means that once the settings are not changed.

You can use the following function to set the environment variable to a variable

Dotenv::makemutable ();

... You can also use the following function to make it no longer variable

Dotenv::makeimmutable ();
The requirement variable must be set

Using Dotenv, you can specify that the ENV variable must be set, and throw an exception if there is no setting. This is very useful for people who cannot run if the program lacks this variable.

Use the following syntax:

Dotenv::required (' Database_dsn ');

or an array to define:

Dotenv::required (Array (' Db_host ', ' db_name ', ' db_user ', ' Db_pass '));

If the ENV variable is missing, dotenv throws a runtimeexception:

Required environment variable missing or value not allowed: ' Db_user ', ' Db_pass '
Allowed Values

You may have seen the above exception information, you can set a possible range of values, so that your environment variables to comply with this rule

Dotenv::required (' Session_store ', Array (' filesystem ', ' Memcached '));

Similarly, if the environment variable is not in this list, you will receive a similar exception message:

Required environment variable missing or value not allowed: ' Session_store '
Notes

You can use # to annotate characters. e.g.

# This is a comment
var= "value" # comment
Var=value # comment
working with Annotations

When a developer clones your code base. They will receive an additional manual one-time step to copy. Env.example and rename to. env and append their own values (or get other sensitive values from other developers).

Phpdotenv is used to build a developer environment but should not be used in a production environment. In a production environment, you need to set up real variables without using the. env file to overload each time you use the request.

This can be done by automating the deployment tools, such as vagrant, Chef, Puppet, or manually through the cloud host, for example: Pagodabox, Heroku. Reference

https://segmentfault.com/a/1190000010886175

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.