18

Is it possible to configure a Silex Application with YAML config files? I bet yes, but how is it done correctly? For instance I want to use different configurations according to the environment, like config.dev.yml and config.prod.yml.

The config file should contain parameters for the app itself and for the registered extensions/services.

Example:

// Debug flag should be set in the config file
$app['debug'] = true;

// Extension options should also be set in config file
$app->register(new Silex\Extension\TwigExtension(), array(
    'twig.path'       => __DIR__.'/views',
    'twig.class_path' => __DIR__.'/vendor/Twig/lib',
));

Do I have to parse the YAML file by myself and set the parameters accordingly or is there a more "magic" way to do this?

fbrandel
  • 1,605
  • 3
  • 17
  • 18
  • Just found this extension on GitHub: https://github.com/tyaga/LoadConfigExtension/blob/master/LoadConfigExtension.php – fbrandel Sep 20 '11 at 17:13
  • That doesn't set anything on your app/container though. It just creates an `$app['config']` service. – igorw Sep 21 '11 at 19:18
  • Ok, but it just did what I was looking for... a way to load a yml config file. Nothing magical here, but works ;) (Be careful when using this extension, it uses the "old" ExtensionInterface. This has changed in the new Silex version) – fbrandel Sep 24 '11 at 12:29

3 Answers3

37

First of all, add the Symfony Yaml component to your composer.json

"symfony/yaml": "2.1.*@dev",

Use the right version choosing directly from the packagist page: https://packagist.org/packages/symfony/yaml

Now, you can add the deralex YamlConfigProvider, a simple and useful Silex provider. Add it to your composer.json:

"deralex/yaml-config-service-provider": "1.0.x-dev"

Here the official github page: https://github.com/deralex/YamlConfigServiceProvider

Here the packagist page: https://packagist.org/packages/deralex/yaml-config-service-provider

UPDATE

Install the dependencies with ./composer.phar update command and finally add these lines to your app file:

$app = new Silex\Application();
$app->register(new DerAlex\Silex\YamlConfigServiceProvider(__DIR__ . '/settings.yml'));

Now, for example, you can do this:

settings.yml

database:
    driver: pdo_mysql
    host: localhost
    dbname: database_name
    user: root
    password: password
    charset: utf8

index.php

$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
    'db.options' => $app['config']['database']
));
Francesco Casula
  • 26,184
  • 15
  • 132
  • 131
  • Do you know if there is similar extension to load a PHP config file? – Fractaliste Sep 19 '14 at 13:13
  • @Fractaliste try https://github.com/igorw/ConfigServiceProvider it speaks php, json, yaml, and toml – ivoba Dec 01 '14 at 09:05
  • 1
    It isn't needed to require `symfony/yaml` in the own *composer.json* because it is already required by `deralex/yaml-config-service-provider` ;-) – stollr Aug 27 '15 at 07:44
  • I know que question is old, but the `"deralex/yaml-config-service-provider"` is not compatible with the last version of the `"symfony/yaml"`. The use of the symfony/yaml was enought to me – cmnardi Aug 23 '16 at 14:28
  • An active-ish fork of ConfigServiceProvider with Silex 2.0 support can be found at: https://github.com/reva2/ConfigServiceProvider – allejo Dec 29 '16 at 05:14
3

This package in the answer does not work for Silex 2.0 that is why I've created package that works for Silex 2.0 and Symfony/Yaml 3.1. Maybe someone looking for this answer will find it useful

https://packagist.org/packages/rpodwika/yaml-config-service-provider

to use run command

composer require rpodwika/yaml-config-service-provider

or add

"rpodwika/yaml-config-service-provider" : "dev-master" 

to your composer.json

github link https://github.com/rpodwika/yaml-config-service-provider

to use:

<?php

require_once __DIR__.'/../vendor/autoload.php';

$app = new Silex\Application();

$app->register(new Rpodwika\Silex\YamlConfigServiceProvider("settings.yml"));

echo $app['config']['database']['driver'];
Robert
  • 19,800
  • 5
  • 55
  • 85
0

The LoadConfigExtension described by @fbrandel (above in comments) allows you to share the yml loader config service.

Ronan
  • 4,311
  • 3
  • 19
  • 14