The question refers to a database connection. I’m new to the slim framework, and there was a question, should I use a container to connect to the database or create a Connection class that connects to the database and extend into a model? Both deliver the same result, but as a beginner it’s always good to ask questions
Example 1: Using a Class
Create the connection class
Call the connection class in a Model class
namespace App\models;
use App\models\Connection;
class Model
{
protected $connect;
protected $table;
public function __construct()
{
$this->connect = Connection::connect();
}
...
Uses the Post class that extends the Model class that owns the database connection
Example 2 : Using container
Register the container DB
Create a Mapper class to do dependency injection
abstract class Mapper
{
protected $db;
public function __construct(\PDO $pdo)
{
$this->db = $pdo;
}
}
Registers controller dependency container
$container['Source\Controller\TicketController'] = function ($c) {
return new TicketController($c['view'], $c['logger'], $c['db']);
};
Use direct container in controller class
I am currently using slim framework 3.12, I know there is slim 4, but for learning I am starting at 3.
Since this is my first question on the forum, and I don’t know English, probably something in the text may not be grammatically correct because I used google translator.
If the question is large I can summarize, because I was concerned to give as much detail as possible.