Config and Env Classes
Table of contents
1. Overview Table of Contents
We use two classes called Config and Env to manage configuration files and environmental variables. These classes are discussed in detail below.
2. Env Utility Table of Contents
The Env utility helps load and manage environment variables from a .env
file.
A. π Features
- β Loads environment variables from a .env file.
- β Automatically converts βtrueβ / βfalseβ to booleans.
- β Automatically converts numeric values to integers or floats.
- β
Provides a
get()
method to retrieve variables with default values.
B. π§ Installation & Setup
The Env utility is automatically included in your framework. To ensure .env is loaded, call:
use Core\Lib\Utilities\Env;
Env::load(ROOT . '/.env');
An example .env
file:
APP_NAME="My Custom App"
DEBUG=true
MAX_USERS=100
ENABLE_FEATURE=false
C. π Usage
π Getting an Environment Variable
echo Env::get('APP_NAME'); // Outputs: "My Custom App"
π Providing a Default Value
echo Env::get('NON_EXISTENT_KEY', 'Default Value');
β Example Output
.env Value |
Env::get('KEY') Output |
---|---|
DEBUG=true |
true (boolean) |
MAX_USERS=100 |
100 (integer) |
ENABLE_FEATURE=false |
false (boolean) |
PI=3.14 |
3.14 (float) |
D. π Debugging
If Env::get('SOME_KEY')
returns null
, check:
- The
.env
file exists at the correct location. - The
.env
file follows the formatKEY=VALUE
. - Restart the server (sudo systemctl restart apache2) or (sudo systemctl restart nginx). If using
php console serve
pressctrl+c
and restart PHP server.
3. Config Utility Table of Contents
The Config
utility helps manage configuration files stored in the config/
directory.
A. π Features
- β Loads configuration files dynamically.
- β
Uses dot notation (
config('app.name')
) for nested values. - β Provides default values when keys are missing.
B. π§ Installation & Setup
The Config
utility is automatically included in your framework. To load all config files, call:
use Core\Lib\Utilities\Config;
Config::load(ROOT . '/config');
π Example config/app.php File
return [
'name' => Env::get('APP_NAME', 'DefaultApp'),
'debug' => Env::get('DEBUG', false),
'timezone' => Env::get('TIME_ZONE', 'UTC'),
];
C. π Usage
π Getting a Config Value
echo Config::get('app.name'); // Outputs: "My Custom App"
π Providing a Default Value
echo Config::get('database.host', '127.0.0.1');
π Example Config Files config/app.php
return [
'name' => Env::get('APP_NAME', 'My App'),
'debug' => Env::get('DEBUG', false),
];
config/database.php
return [
'host' => Env::get('DB_HOST', '127.0.0.1'),
'port' => Env::get('DB_PORT', 3306),
'username' => Env::get('DB_USER', 'root'),
'password' => Env::get('DB_PASSWORD', ''),
];
D. π Advanced Usage
π Storing Dynamic Configuration
Config::set('app.debug', true);
echo Config::get('app.debug'); // Outputs: true
4. π Best Practices Table of Contents
- βοΈ Use
.env
for sensitive credentials (API keys, database passwords). - βοΈ Use
Config::get()
instead of hardcoding values. - βοΈ Ensure all
config/*.php
files return arrays.
5. π Debugging Table of Contents
If Config::get('app.name')
returns null
, check:
Config::load(ROOT . '/config');
was called before usingConfig::get()
.- The file exists in
config/
and returns an array. - Restart the server if needed.
6. π― Conclusion Table of Contents
Env
Utility loads environment variables dynamically.Config
Utility centralizes configuration management.- Use
Env::get()
for.env
values andConfig::get()
for app settings.