Skip to content

Examples

Aleksandr Ivanov edited this page Jan 21, 2021 · 5 revisions

Basic usage

# config/app.yml
debug: true
database:
  driver: pdo_mysql
  host: localhost
$app = new \Silex\Application(['debug' => false]);
$app->register(new Misantron\Silex\Provider\ConfigServiceProvider(
    [__DIR__ . '/config/app.yml']
));
var_dump(
    $app['debug'], // true
    $app['database'] // ['driver' => 'pdo_mysql', 'host' => 'localhost']
);

Configs merging

# config/base.yml
debug: true
database:
  driver: pdo_sqlite
  host: file::memory:
  port:
# config/prod.yml
debug: false
database:
  driver: pdo_mysql
  host: db.instance.host
  port: 3305
$app = new \Silex\Application();
$app->register(new Misantron\Silex\Provider\ConfigServiceProvider(
    [
        __DIR__ . '/config/base.yml',
        __DIR__ . '/config/prod.yml',
    ]
));
var_dump(
    $app['debug'], // false
    $app['database'] // ['driver' => 'pdo_mysql', 'host' => 'db.instance.host', 'port' => 3305]
);

Placeholders replacement

Each string surrounded by % symbol will be replaced by values from replacement dictionary.

// config/app.php
return [
    'debug' => true,
    'cert.root' => '%cert_root%',
];
$app = new \Silex\Application();
$app->register(new Misantron\Silex\Provider\ConfigServiceProvider(
    [__DIR__ . '/config/app.json'],
    [
        'cert_root' => '/opt/cert', // value for replacement
    ]
));
var_dump(
    $app['debug'], // true
    $app['cert.root'] // '/opt/cert'
);

Environment variables

# .env
APP_DEBUG=false
# config/app.yml
debug: '%env(bool:APP_DEBUG)%'
$app = new \Silex\Application(['debug' => true]);
$app->register(new Misantron\Silex\Provider\ConfigServiceProvider(
    [__DIR__ . '/config/app.yml']
));
var_dump($app['debug']); // false

Config key aliases

Mixed config types

Clone this wiki locally