PHP code example of rcalicdan / config-loader

1. Go to this page and download the library: Download rcalicdan/config-loader library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rcalicdan / config-loader example snippets


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');

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);



return [
    'name' => env('APP_NAME', 'My Application'),
    'env' => env('APP_ENV', 'production'),
    'debug' => env('APP_DEBUG', false),
    'url' => env('APP_URL', 'http://localhost'),
];

use Rcalicdan\ConfigLoader\Config;

// Examples:
Config::get('app.name');
Config::loadFromRoot('extra_config'); // Loads extra_config.php from root
Config::set('app.debug', true);
Config::all();

// Get a value
$value = config('app.name');

// Get the instance
$loader = config();

// Loads 'settings.php' from project root into the 'settings' key
$settings = configRoot('settings'); 

// Loads 'legacy.php' from root but stores it under the 'app.legacy' key
configRoot('legacy', 'app.legacy');

// Project Root contains: payment.php returning ['stripe' => ['key' => '123']]

// 1. Using the Facade
Config::loadFromRoot('payment'); 
// Access it:
$key = Config::get('payment.stripe.key');

// 2. Using the Helper with a custom key
configRoot('payment', 'gateways');
// Access it:
$key = config('gateways.stripe.key');

// Temporarily switch to the sqlite database for a test
Config::set('database.default', 'sqlite');

protected function tearDown(): void
{
    Config::reset();
}