Download the PHP package wpscholar/phpdotenv without Composer
On this page you can find all versions of the php package wpscholar/phpdotenv. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download wpscholar/phpdotenv
More information about wpscholar/phpdotenv
Files in wpscholar/phpdotenv
Package phpdotenv
Short Description Loads environmental variables from a .env file.
License GPL-2.0-or-later
Informations about the package phpdotenv
PHP dotenv
A .env
file parsing and loading library for PHP.
Automatically loads variables into a number of contexts:
getenv()
(default)$_ENV
(default)$_SERVER
(default)apache_getenv
(optional)- PHP constants (optional)
- Global variables (optional)
- A custom config array (optional)
Why?
You should never store sensitive credentials in your code. Storing configuration in the environment is one of the tenets of a twelve-factor app. Anything that is likely to change between deployment environments – such as database credentials or credentials for 3rd party services – should be extracted from the code into environment variables.
Requirements
- PHP 5.4
Installation
Using Composer, run composer require wpscholar/phpdotenv
.
Make sure you have a line in your code to handle autoloading:
Usage
Create a new loader and use any of the available methods to help customize your configuration:
It is possible to create multiple instances of the loader, each loading a different .env file and loading variables into different contexts.
Custom Configuration Array Example Usage
WordPress wp-config.php
Example Usage
Creating a .env
File
Sample .env
file for the wp-config.php
example:
Explore all the features of the .env
file parser.
Rules to Follow
When using phpdotenv
, you should strive to follow these rules:
- Add your
.env
file to a.gitignore
file to prevent sensitive data from being committed to the project repository. - Use a
.env.example
to set a default configuration for your project. This allows your development team to override defaults in a method that works for their local environment. - Always set sane defaults when possible.
- Where necessary, add comments to credentials with information as to what they are, how they are used, and how one might procure new ones.
-
As
phpdotenv
uses more lax procedures for defining environment variables, ensure your .env files are compatible with your shell. A good way to test this is to run the following: - When possible, avoid running
phpdotenv
in production settings. Instead, set environment variables in your webserver, process manager or in bash before your app loads.