Get easily access to environment variables.
Open a command prompt into your project root folder and run:
composer require prinx/dotenv
// Require composer autoload file if it has not been done yet.
require_once __DIR__ . '/path/to/vendor/autoload.php';
/*
* Retrieve an environment variable. Returns null if variable not found.
*/
$hostname = env('DEV_DB_HOST');
/*
* Retrieve an environment variable. Returns default value passed as second argument if variable not found
*/
$port = env('DEV_DB_PORT', 3306);
/*
* Add a variable to the current loaded environment (will not save in the .env file)
*/
addenv('LOG_LEVEL', 'info');
/*
* Wrtie variable to the env file.
* Will also automatically load the variable into the current environment.
* If the file already contains the variable, the variable will be overwritten.
*/
persistenv('LOG_LEVEL', 'warn');
persistenv('LOG_LEVEL', 'debug');
persistenv('LOG_LEVEL', 'info');
/*
* Get all environment variables
*/
env()
// OR
allenv();
The .env file format will be:
VARIABLE_NAME=value
For example:
SESSION_DRIVER=file
DEV_DB_HOST=localhost
DEV_DB_PORT=3306
PROD_DB_HOST=prod_db_ip
PROD_DB_PORT=3308
As standard, the variable name is capital letter with underscores to separate words.
You can write comments in your .env file by preceding the comment by a hash (#
).
Example:
# Supported: file|database
SESSION_DRIVER=file
By default, env variable will be retrieved as string, except booleans, and null.
You can use quotes to surround strings.
APP_NAME=My app
# or
APP_NAME="My app"
DB_HOST=173.0.0.0
# or
DB_HOST="173.0.0.0"
The values true
, "true"
or 'true'
, will be got as the boolean true
.
The values false
, "false"
or 'false'
will be got as the boolean false
.
# Will be got as a boolean true
APP_DEBUG=true
# Will be got as a boolean false
APP_DEBUG=false
Same as:
APP_DEBUG="true"
APP_DEBUG="false"
The values null
, "null"
or 'null'
, will be got as null
.
# Will be got as a null
APP_DEBUG=null
APP_DEBUG="null"
You can refer to the value of another variable in your .env file by putting the name of the variable you are referring to variable inside ${}:
# .env
SESSION_DRIVER=mysql
MESSAGE=App based on ${SESSION_DRIVER} database
// PHP
echo env('MESSAGE'); // App based on mysql database
By default, the package automatically look for the .env file in the project root folder. But you can load the env file from anywhere by using the loadenv
function:
// Require composer autoload file if it has not been done yet.
require_once __DIR__ . '/path/to/vendor/autoload.php';
loadenv('/path/to/somewhere/.env');
// Then everything goes as usual
$apiKey = env('API_KEY');
You can also get or set a variable using the Dotenv class instance:
The Dotenv instance can be accessed by calling the dotenv()
function:
$dotenv = dotenv();
$hostname = dotenv()->get('DEV_DB_HOST');
// With a default value
$hostname = dotenv()->get('DEV_DB_HOST', 'localhost');
$hostname = dotenv()->all();
// or use get without any parameter
$hostname = dotenv()->get();
dotenv()->add('GUEST_NAME', 'john');
dotenv()->persist('GUEST_NAME', 'john');
You can create your own Dotenv instance just by using the Dotenv
class:
use Prinx\Dotenv;
$dotenv = new Dotenv('path/to/.env');
$dotenv->get('VARIABLE');
- Give a star to the repo
☺️ - Fork the repo.
- Correct a bug, add a new feature.
- Write tests.
- Create a pull request.