PHP code example of g4mr / configs

1. Go to this page and download the library: Download g4mr/configs 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/ */

    

g4mr / configs example snippets


    
    use G4MR\Configs\Config;
    use G4MR\Configs\Loaders\YamlLoader;

    $config = new Config(new YamlLoader(__DIR__ . '/config'));

    //loads ./config/database.yml as an array block
    $db_config = $config->get('database', false);
    if($db_config !== false) {
        echo $db_config['dbname'];
    }

    
    use G4MR\Configs\Config;
    use G4MR\Configs\Loaders\YamlLoader;

    $config = new Config(new YamlLoader(__DIR__ . '/config'));

    //example using stash (http://www.stashphp.com) caching
    $pool = new Stash\Pool();

    $db_stash   = $pool->getItem('db/config');
    $db_config  = $db_stash->get();

    if($db_stash->isMiss()) {
        $db_config = $config->getItem('database');
        $db_stash->set($db_config, 60 * 5); //cache data for 5 minutes
    }

    $dbname = $db_config->get('dbname', null);
    $dbuser = $db_config->get('username', null);
    $dbpass = $db_config->get('password', null);
    $dbhost = $db_config->get('host', 'localhost');

    
    use G4MR\Configs\Config;
    use G4MR\Configs\Loaders\YamlLoader;

    $config = new Config(new YamlLoader(__DIR__ . '/config'));

    //loads ./config/database.yml as an item object
    $db_config = $config->getItem('database');
    $db_config->set('db.user', 'root');
    $db_config->set('db.pass', 'root');
    $db_config->set('db.host', 'localhost');
    $db_config->set('db.name', 'mydatabase');

    $dbhost = $db_config->get('db.user'); // 'root'

    
    use G4MR\Configs\Config;
    use G4MR\Configs\Loaders\YamlLoader;

    $config = new Config(new YamlLoader(__DIR__ . '/config'));

    //loads ./config/connection.yml as an item object
    $conn = $config->getItem('connection');
    $conn->db = [
        'host' => 'localhost',
        'user' => 'root',
        'pass' => 'root',
        'name' => 'dbname'
    ];

    print_r($conn->get('db'));
    //echo $conn->get('db.user');

    //or

    print_r($conn->db);