Config

Simple yet expressive configuration library for PHP apps

$ composer require league/config

Author Latest Version Total Downloads Software License Build Status Coverage Status Quality Score

Highlights

league/config helps you define configuration arrays with strict schemas and easy access to values with dot notation.

  1. Easy-to-use, expressive API

  2. Supports configuration schemas, allowed types, validation, deprecation, and more

  3. Simplified access to nested configuration options using `parent.child` syntax

  4. Define everything with code - no XML, YAML, or JSON required

Features

Expressive, Fluent API

use League\Config\Configuration;
use Nette\Schema\Expect;

$config = new Configuration([
    'database' => Expect::structure([
        'driver' => Expect::anyOf('mysql', 'postgresql', 'sqlite')->required(),
        'host' => Expect::string()->default('localhost'),
        'port' => Expect::int()->min(1)->max(65535),
        'database' => Expect::string()->required(),
        'username' => Expect::string()->required(),
        'password' => Expect::string()->nullable(),
    ]),
]);

Easily Access Nested Values

echo $config->get('database.driver');

// or using slashes, if you prefer that syntax:
echo $config->get('database/driver');

Set Options Individually Or Together

use League\Config\Configuration;

$config = new Configuration([/*...*/]);

$config->merge([
    'database' => [
        'driver' => 'mysql',
        'port' => 3306,
        'host' => 'localhost',
        'database' => 'myapp',
        'username' => 'myappdotcom',
        'password' => 'hunter2',
    ],
]);

if ($_ENV['APP_ENV'] === 'prod') {
    $config->set('payment_gateway.test_mode', false);
}

Combine Multiple Schemas Into One

use League\Config\Configuration;

$config = new Configuration();
$config->addSchema('database', DB::getConfigSchema());
$config->addSchema('logging', Logger::getConfigSchema());
$config->addSchema('mailer', Mailer::getConfigSchema());

Questions?

Config was created by Colin O'Dell. Find him on Twitter at @colinodell.