The Slim Framework support forum has moved to This Tender forum is no longer maintained or monitored.

Database Config

Rich's Avatar


02 Mar, 2012 05:25 PM

I am using both mongodb and MySQL for various purposes in a relatively large app built with Slim. Structure is based around the large app architecture example given on the Slim website.

My question is this: Where is best to set my application configuration, e.g. db connection parameters, memcache params etc. Rather than simply specifying them at the top of index.php.

Many thanks,


  1. 1 Posted by hexa2k9 on 02 Mar, 2012 07:22 PM

    hexa2k9's Avatar


    I usually put all that database stuff into an included file as a function which looks about like that:

    function getAppConfig() {
      $appConfig = array(
        'mysql' => array(
          'username' => 'fixme',
          'password' => 'fixme',
          // ...
        'redis' => array(
          'host'     => 'localhost',
          'port'     => ....
        // other settings
      return $appConfig;
    function getAppSettings() {
      $config = getAppConfig();
      // connect to mysql using pdo
      // connect to redis
      // put all that stuff into another array
      return $array;

    Turned out that this works quite great for me. Any of my routes get required connections, settings & stuff by calling $settings = getAppSettings();

  2. 2 Posted by Rich on 04 Mar, 2012 10:36 AM

    Rich's Avatar

    Thanks, that's helpful!

    I guess another approach would be to use a simple config class. It would be nice if we could pass an instance to the Slim constructor, so our config is available throughout.

    Currently, i'm passing my config vars to Slim::config().

    Does anyone else have any different approaches to loading app config. e.g. from an XML or Yaml file etc.

  3. 3 Posted by hexa2k9 on 04 Mar, 2012 11:03 AM

    hexa2k9's Avatar


    My Slim Config does look like that too, by the way. It's a function as well ..

    I played around with a Json-Object which was loaded from a Remote-Server using http, however it turned out that was not fast enough to get. That can still be done within a larger Application with multiple nodes on a LAN w/ very fast connections in between. With geographically distributed nodes (which is the case for me) that's not working fine.

    However I liked the approach for setting config values once and have them available instantly on all application servers.


    Am 04.03.2012 um 11:36 schrieb "Rich" <[email blocked]>:

  4. 4 Posted by Rich on 04 Mar, 2012 11:31 AM

    Rich's Avatar

    I would certainly have all config stored in file, on disk. There's no way (or rational reason) using a http request to grab config data for every application request would ever be fast enough. Plus, you would lose any benefit gained from using a bytecode cache (apc etc). My approach is to conditionally load a config document (xml, yaml or indeed json) based on the application environment.

    For multi-server setups, the core codebase (including config files) is deployed to each machine. There's no reason to willingly introduce additional network traffic. However, this is out of the scope of my initial question, which pertained to structuring application code using Slim. Thanks for your help :)

  5. 5 Posted by Bill on 04 Mar, 2012 01:04 PM

    Bill's Avatar

    @hexa2k9, you could always cache the files, if you were worried about a delay.

    The config file doesn't really change that much. I think it's probably the least of your worries!

  6. Support Staff 6 Posted by Josh Lockhart on 24 Mar, 2012 08:06 PM

    Josh Lockhart's Avatar

    I always put my configuration details into an included file so they are not stored in public-accessible PHP files. Should the Apache config become corrupted and output PHP as plain text, the world won't know my db credentials, etc.

    Slim's configuration is just a key-value store... you can push anything you want into it. Your config file may look like this:

    $config = array(
        'db' => array(
            'user' => 'test',
            'pass' => 'test'

    And you primary Slim index.php file may look like this:

    require '../config.php';
    $app = new Slim(array(
        'custom' => $config

    You can then retrieve your configuration anywhere in your Slim app with:

    $config = $app->config('custom');
    echo $config['db']['user'];

    But that's just one way among many. Just wanted to make sure you knew how Slim's configuration worked.

  7. Josh Lockhart closed this discussion on 24 Mar, 2012 08:06 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac