Config and Env Classes

Table of contents

  1. Overview
  2. Env Utility
  3. Config Utility
  4. Best Practices
  5. Debugging
  6. Conclusion


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 format KEY=VALUE.
  • Restart the server (sudo systemctl restart apache2) or (sudo systemctl restart nginx). If using php console serve press ctrl+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 using Config::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 and Config::get() for app settings.