You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-**Base Path Handling**: The base path of the project is automatically set to the root level of the project, but you can provide a custom base path when initializing the class.
48
-
-**Environment Groups**: Group environment variables based on their prefix.
49
-
-**File Validation**: Ensures that the specified .env file(s) exist in the project directory.
50
-
-**Environment Update**: Synchronizes environment variables with the PHP $_SERVER and $_ENV superglobals.
51
47
52
-
## Configuration
53
-
- You can customize the base path for locating environment files by passing it to the constructor:
48
+
- ✅ Automatically uses the project root, but allows setting a custom path when initializing.
49
+
- ✅ Load from multiple .env files
50
+
- ✅ Customize file names and base path
51
+
- ✅ Support for grouped environment variables
52
+
- ✅ Auto-sync with `$_ENV`, `$_SERVER`, and `getenv()`
53
+
- ✅ Reload environment at runtime
54
+
- ✅ Check if a variable exists with `has()`
55
+
- ✅ Supports default fallback values
56
+
57
+
## Constructor Signature
54
58
```php
55
-
$dotenv = new Dotenv(null, '/custom/path/');
59
+
public function __construct(
60
+
array|string|null $files = null,
61
+
string|null $envKey = null,
62
+
bool $overwrite = true
63
+
)
56
64
```
57
-
58
-
- If no file is provided, the Dotenv class will search for the following default files in the specified or root directory:
59
-
60
-
- .env
61
-
- .env.local
62
-
- .env.development
63
-
- .env.production
64
-
- .env.dev
65
-
- .env.prod
65
+
-`$files`: Optional string or array of .env file names. Defaults to standard files like `.env`, `.env.local`, etc.
66
+
-`$envKey`: Custom key for detecting the app environment (e.g., `APP_ENV`)
67
+
-`$overwrite`: Whether to overwrite existing variables (true by default)
68
+
69
+
70
+
- If no file is provided, the Dotenv class will search for the following default files in root directory:
71
+
- .env
72
+
- .env.local,
73
+
- .env.development
74
+
- .env.production
75
+
- .env.testing
76
+
- .env.dev
77
+
- .env.prod
78
+
- .env.test
79
+
- .env.staging
66
80
67
81
## Usage
68
82
69
83
### Loading Environment Variables
70
-
- To load environment variables, simply initialize the Dotenv class.
71
-
- By default, it looks for common .env files (such as .env, .env.local, .env.production, etc.) in the project root directory.
84
+
- You can pass your environment file(s) with either a full path or just the file name.
85
+
- If you pass a full path, the file will be loaded directly from that path.
86
+
- If you pass just the file name, it will be resolved relative to the project root directory.
72
87
73
88
```php
74
89
<?php
75
90
76
-
require_once "vendor/autoload.php";
77
-
78
91
use NAL\Dotenv\Dotenv;
79
92
80
-
// Initialize Dotenv and load environment variables
81
-
$dotenv = new Dotenv();
82
-
```
93
+
// Load a file using its full path
94
+
$dotenv = new Dotenv('/home/user/project/config/.env.custom');
83
95
84
-
- You can also specify custom environment files to load by passing the file name(s) to the constructor:
85
-
```php
86
-
$dotenv = new Dotenv('.env.testing');
87
-
```
96
+
// Load a file from the project root by name
97
+
$dotenv = new Dotenv('.env.production');
88
98
89
-
- Or load multiple files:
90
-
```php
91
-
$dotenv = new Dotenv(['.env.dev', '.env.prod']);
99
+
// Load multiple files
100
+
$dotenv = new Dotenv(['.env.local', '/home/user/project/.env.override']);
92
101
```
93
102
94
103
### Accessing Environment Variables
95
-
- Once the environment files are loaded, you can retrieve environment variables using the get() method. If no key is provided, it will return all variables.
96
104
97
105
```php
98
106
// Get a specific variable
99
-
$appName = $dotenv->get('APP_NAME');
107
+
$dotenv->load();
100
108
101
-
// Get all variables
102
-
$envVariables = $dotenv->get();
109
+
$host = $dotenv->get('DB_HOST'); // Get a variable
110
+
$debug = $dotenv->get('DEBUG', false); // With fallback
111
+
$all = $dotenv->get(); // Get all loaded variables
103
112
```
104
113
105
-
- You can also provide a default value if the key is not found:
114
+
### Check Existence
106
115
```php
107
-
$debugMode = $dotenv->get('DEBUG_MODE', false);
116
+
if ($dotenv->has('APP_SECRET')) {
117
+
// APP_SECRET is defined
118
+
}
108
119
```
109
120
110
121
### Grouping Environment Variables
111
122
- You can access grouped environment variables with the group() method.
112
123
- This can be useful if you have variables structured by prefix (e.g., APP_NAME, APP_ENV).
113
124
114
125
```php
115
-
$appGroup = $dotenv->group('APP');
126
+
$grouped = $dotenv->group('APP');
127
+
// e.g., ['APP_NAME', 'APP_ENV', 'APP_KEY']
116
128
```
117
129
118
130
### Reloading Environment Variables
119
131
- You can reload environment variables by calling the `reload()` method. This method clears the previously loaded variables and reloads them from the specified files.
120
132
```php
121
-
$dotenv->reload();
133
+
$dotenv->reload(); // Clear and reload loaded files
0 commit comments