Slim 3 framework: good mysql practice

I'm trying to figure out what the best practice might be to use a mysql connection instance via dependency injection in Slim Framework 3.

I am using Rob Allens skeleton and added these lines to app / dependencies.php

$container['db'] = function ($c) {
    $settings = $c['settings']['LOCAL_DB'];
    return new \App\Action\DatabaseAction($settings['DB_HOST'],$settings['DB_DATABASE'],$settings['DB_USERNAME'],$settings['DB_PASSWORD']); 
};


$container['App\Action\HelloAction'] = function ($c) {
    return new App\Action\HelloAction($c['view'], $c['db']);
};

      

And then use it in the class like this:

$data['test'] = $this->db->one('SELECT * FROM database');

      

It works well, but I can't be sure if it's a good way in terms of good practice, resource usage, memory ...

Any help / advice would be appreciated.

+3


source to share


1 answer


This is a great way to do it as it follows good dependency injection practice.

In terms of memory usage, you also ensure that the database will only be created when needed, not for every query, which is fine as well.



Note that the class that creates the database does not need to be named DatabaseAction

, although since this is not an action, it is the creator of the database instance. I would call it something likeApp\Database

0


source







All Articles