Configuration

This fork of the Slim framework identifies 2 diffent configurations.

The reason that this has been split is motivated by the fact that it will be much easier to install this fork into another application.
You have to change the database and mail settings anyway, but you don't want to move the application settings to that other application. So you would simply clear
the array in the application config.

Framework

The settings for the framework (or Slim itself) can be found in the folder 'config/framework'.
Here you will find 2 folders:

Within these folder you will find a file named 'config.php'. It contains simply an array of configuration settings that are used
to control functionality within your Slim installment. That is, it contains settings that control functionality like debugging, mail, database settings, language support etc...

mode.php

As the folders 'development' and 'production' imply, you can have settings for development and production. This is controlled by the 'mode.php' file in the root of your server.
When you put your application in production, you have to change the value within this file to 'production'.

Application

You will find the same structure as in the Framework section in the folder 'config/app'. The difference between the Framework settings and the Application settings is that
the settings in this file are purely meant to control your application's logic. In this file you would find f.e. customer_prefix, firm_name, location and so on.

How to access the settings

The Framework settings can be accessed through the $container object.

$db = $container->settings->get('db');

$i18nConfig = $container->settings->i18n['config'];

For the Application settings this Slim 3 fork is using the DavidePastore\Slim\Config package that is based on the Hassankhan/Config. The application settings are injected into the container 'config'. By default it also injected in every route.

$app->add($container->get('config'));

You can modifiy this by changing the 'App.js'.

Usage

In most cases you want to register DavidePastore\Slim\Config for a single route, however,
as it is middleware, you can also register it for all routes.

Register per route

$app = new \Slim\App();

// Fetch DI Container
$container = $app->getContainer();

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

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

$app->run();

Register for all routes

$app = new \Slim\App();

// Fetch DI Container
$container = $app->getContainer();

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

// 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('security.secret');
});

$app->post('/bar', function ($req, $res, $args) {
  //Here you have your configuration
  $config = $this->config->getConfig();
  $ttl = $config->get('app.timeout', 3000);
});

$app->run();
Last update: March 15th at 9:45am