The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Slim 3 and pdo usage

chanh.ong's Avatar

chanh.ong

03 Mar, 2016 09:41 PM

I have seen and use database in Slim 3 with the code below:

in settings.php
        'pdo' => [
            'dsn' => 'mysql:host=localhost;dbname=dbnotes;charset=utf8',
            'username' => 'uname',
            'password' => 'upass',
        ],

In dependencies.php
$container['pdo'] = function ($c) {
    $settings = $c->get('settings')['pdo'];

    return new PDO($settings['dsn'], $settings['username'], $settings['password']);
};
$container['App\Action\HomeAction'] = function ($c) {
    return new App\Action\HomeAction($c->get('view'), $c->get('logger'), $c['pdo']);
};

In Routes.php
$app->get('/', 'App\Action\HomeAction:dispatch')->setName('homepage');

In ActionController.php
    public function dispatch(Request $request, Response $response, $args)
    {
        $this->logger->info("Home page action dispatched");

        $stmt = $this->pdo->prepare('SELECT * FROM books ORDER BY id ASC');
        $stmt->execute();
        $result = array();
        while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
            $result[] = array($row['id'], $row['title']);
        }
        print_r($result);
}

It works but I wonder is there a better way?
Is there a way to make something like $SlimApp->db->query(), ->connect(), etc and call $SlimApp from anywhere?
If there is how is that done?

Thanks in advance.

  1. 1 Posted by chanh.ong on 03 Mar, 2016 10:00 PM

    chanh.ong's Avatar

    I try one method, instead of passing individual variable I just pass the entire $SlimApp variable then I just use it with whatever variable in $SlimApp.

    In dependencies.php

    In HomeAction Controller
    $container['App\Action\HomeAction'] = function ($c) {
        return new App\Action\HomeAction($c);
    };

    final class HomeAction
    {
        private $app;

        public function __construct($app)
        {
            $this->app = $app;
        }

        public function dispatch(Request $request, Response $response, $args)
        {
            $this->app->logger->info("Home page action dispatched");

            $stmt = $this->app->pdo->prepare('SELECT * FROM books ORDER BY id ASC');
            $stmt->execute();
            $result = array();
            while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
                $result[] = array($row['id'], $row['title']);
            }
            print_r($result);
            $this->app->view->render($response, 'home.twig');
            return $response;
        }
    }

    Is this a good way?

    Thanks

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

Generic

? 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