https://github.com/fkrzski/dotenv
Library that adds the ability to access variables from '.env', $_ENV, and $_SERVER using the getenv() function
https://github.com/fkrzski/dotenv
configuration dotenv env environment environment-variables php
Last synced: over 1 year ago
JSON representation
Library that adds the ability to access variables from '.env', $_ENV, and $_SERVER using the getenv() function
- Host: GitHub
- URL: https://github.com/fkrzski/dotenv
- Owner: fkrzski
- License: mit
- Created: 2022-01-15T11:27:15.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-01-31T13:14:43.000Z (over 4 years ago)
- Last Synced: 2025-01-04T01:58:46.699Z (over 1 year ago)
- Topics: configuration, dotenv, env, environment, environment-variables, php
- Language: PHP
- Homepage:
- Size: 42 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Dotenv
Library that adds the ability to access variables from '.env', $_ENV, and $_SERVER using the getenv() function
# Installation
```shell
composer require fkrzski/dotenv
```
# Usage
### Basics
Add your application configuration variables to `.env` file in your project. Next add `.env` to `.gitignore` file! You should create a `.env.example` file to have a skeleton with variable names for your contributors
```shell
APP_NAME="My App Name" # My app name
API_KEY=YourApiKey # My api key
```
### Include ` Dotenv ` class
```php
use Dotenv\Dotenv;
```
### Load ` .env ` variables
```php
$dotenv = new Dotenv('.env');
$dotenv->start();
```
### Custom path or file name
```php
$dotenv = new Dotenv('path/to/file/myenvfile.env');
// Now you are using myenvfile.env from /path/to/file folder
```
### Many `.env` files
```php
$dotenv = new Dotenv('path/to/file/myenvfile.env', 'path/to/file/mysecondenvfile.env');
```
### Retrieving variables values
```php
echo getenv('APP_NAME');
echo $_SERVER['APP_NAME'];
echo $_ENV['APP_NAME'];
// output: My App Name
```
### Overwrtitting a variable
`.env` file
```shell
APP_NAME="App Name"
API_KEY=ApiKey
APP_NAME="Second App Name"
API_KEY=SecondApiKey
```
PHP file
```php
$dotenv->start(['APP_NAME']);
echo getenv('APP_NAME');
echo getenv('API_KEY');
// Output:
// Second App Name
// ApiKey
```
Second possibility
`.env` file
```shell
APP_NAME="App Name"
API_KEY=ApiKey
APP_NAME="Second App Name"
API_KEY=SecondApiKey
```
PHP file
```php
$dotenv->start(['*']);
echo getenv('APP_NAME');
echo getenv('API_KEY');
// Output:
// Second App Name
// SecondApiKey
```
### Validating and requiring variables
`.env` file
```shell
APP_NAME="App Name"
PHONE_NUMBER=111222333
```
PHP file
```php
$dotenv->start();
$dotenv->validator()->validate([
'APP_NAME' => 'required|alnum',
'PHONE_NUMBER' => 'required|integer',
]);
/* All validating rules:
* - required
* - letters (Letters and spaces only)
* - alnum (Letters, numers and spaces)
* - integer
* - boolean (true/false)
* - float
*/
```
### Put single variable
```php
Dotenv::single('VAR', 'value');
echo getenv("VAR");
```