DavidePastore / Slim-Config

A file configuration loader that supports PHP, INI, XML, JSON, and YML files for the Slim Framework. It internally uses https://github.com/hassankhan/config.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is YML also allowed?

visualcookie opened this issue · comments

Hi,

just a question. In the README.md you set the things up with a config.json. Can this also be a YML file or has it to be a JSON? If so, would it be easy to add YML as supported config file.

Hi @visualcookie, yes YML files are supported too. For example you can do:

// Register provider
$container['config'] = function () {
  //Create the configuration
  return new \DavidePastore\Slim\Config\Config('config.yml');
};

// Register middleware for all routes
// If you are implementing per-route checks you must not add this
$app->add($container->get('config'));

$app->get('/foo', function ($req, $res, $args) {
  //Here you have your configuration
  $config = $this->config->getConfig();
  $secret = $config->get('application.secret');
});

Your YML file could be:

application:
    name: configuration
    secret: s3cr3t
host: localhost
port: 80
servers:
- host1
- host2
- host3

Yeey, awesome. Thank you for doing this. Before all I did was creating a PHP Array, where I find YML kinda more readable but wasn't able with my knowledge so far, how to do this. 👍

I'm happy that my answer has helped you. There is not a lot of magic behind it (it is using https://github.com/symfony/Yaml). I close this issue. Feel free to reopen if you find any issues with this solution.