PHPackages                             rcalicdan/config-loader - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. rcalicdan/config-loader

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

rcalicdan/config-loader
=======================

A lightweight configuration loader with automatic project root detection and dot notation support

1.2.3(4mo ago)08821MITPHPPHP ^8.3CI passing

Since Oct 21Pushed 4mo agoCompare

[ Source](https://github.com/rcalicdan/config-loader)[ Packagist](https://packagist.org/packages/rcalicdan/config-loader)[ GitHub Sponsors](https://github.com/rcalicdan)[ RSS](/packages/rcalicdan-config-loader/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (8)Dependencies (5)Versions (9)Used By (1)

ConfigLoader
============

[](#configloader)

A lightweight, zero-configuration PHP library for managing environment variables and configuration files with automatic project root detection.

Features
--------

[](#features)

- **Zero Configuration**: Automatically finds your project root without any setup.
- **Singleton Pattern**: Loads configuration only once per request, ensuring high performance.
- **Convention-based**: Works out-of-the-box with a standard `.env` file and `/config` directory structure.
- **Dot Notation**: Access nested configuration values easily (e.g., `database.connections.mysql.host`).
- **Runtime Configuration**: Modify configuration values on the fly for testing or dynamic adjustments.
- **Manual File Loading**: Load specific configuration files from your project root on demand.
- **Static Facade**: A convenient static wrapper (`Config::class`) for easy access anywhere in your code.
- **Helper Functions**: Simple global `config()`, `configRoot()`, and `env()` functions for quick access.
- **Performance Optimized**: All loaded configuration is cached in memory for the duration of the request.

Installation
------------

[](#installation)

```
composer require rcalicdan/config-loader
```

Requirements
------------

[](#requirements)

- PHP 8.3 or higher

Quick Start
-----------

[](#quick-start)

### Basic Usage

[](#basic-usage)

```
use Rcalicdan\ConfigLoader\ConfigLoader;
use Rcalicdan\ConfigLoader\Config;

// Option 1: Get the singleton instance
$config = ConfigLoader::getInstance();
$dbHost = $config->get('database.connections.mysql.host');

// Option 2: Use the static facade for cleaner access
$appName = Config::get('app.name', 'Default App Name');
```

### Using Helper Functions

[](#using-helper-functions)

The library provides global helper functions for the most common use cases. This is the recommended way to interact with the library in most applications.

```
use function Rcalicdan\ConfigLoader\config;
use function Rcalicdan\ConfigLoader\configRoot;
use function Rcalicdan\ConfigLoader\env;

// Access a configuration value
$dbHost = config('database.connections.mysql.host');

// Provide a default if the key doesn't exist
$appName = config('app.name', 'Default App Name');

// Manually load a file from the project root
$customSettings = configRoot('custom_settings.php');

// Access an environment variable directly
$debugMode = env('APP_DEBUG', false);
```

Project Structure
-----------------

[](#project-structure)

ConfigLoader expects your project to follow this standard structure:

```
your-project/
├── vendor/              # Composer dependencies (used to detect the project root)
├── .env                 # Your environment variables
├── custom.php           # Custom config file in root (optional)
├── config/              # Standard Configuration directory
│   ├── app.php
│   ├── database.php
│   └── services/        # Nested directories are supported
│       └── mail.php
└── src/
    └── your-code.php

```

Configuration Files
-------------------

[](#configuration-files)

Create PHP files in the `/config` directory that return an array. You can use the `env()` helper to load values from your `.env` file.

#### `config/app.php`

[](#configappphp)

```
