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

Database Integration

Michael Thomas's Avatar

Michael Thomas

18 Oct, 2012 04:30 PM

I know Slim doesn't provide any kind of database wrapper or integration, but what would be the best way to implement it if I wanted easy access to a single DB object throughout my Slim $app object? Currently, I added a custom config field so I can access it via $app->config("sql.db");, but is there a better or more recommended way?

  1. Support Staff 1 Posted by Brian Nesbitt on 31 Oct, 2012 03:44 AM

    Brian Nesbitt's Avatar

    Without knowing much about your app you could use and put it in the environment for all to access.

    $pimple = new Pimple();
    $pimple['username'] = 'me';
    $pimple['password'] = 'secret';
    // Share one instance of the db
    $pimple['db'] = $pimple->share(function ($pimple) {
        return new Database($pimple['username'], $pimple['pass]);
    $env = $app->environment();
    $env['container'] = $pimple;

    Then to access the db instance from anywhere:

    $env = $app->environment();

    Then of course for testing you can change what the function creates... ie maybe a mock db object.

  2. 2 Posted by Robert von Kuli... on 25 Feb, 2016 02:28 PM

    Robert von Kulig's Avatar

    I use he following to connect to my notorm resources;

    // Define DB resources
    $app->container->singleton( 'apidb', function () { require_once( "../../libs/oconn.php" ); return $db; });

    $apidb = $app->apidb;

        function ( $id ) use ( $app, $apidb ) {

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:


Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

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