Slim 4 DB operations

What is the preferable way to handle CRUD operations in Slim 4? Any example for MySQL?

After some period of reply’s absence, I would try to bring some clearence in my question.
The topic was raised because of Slim 4 beta is released. Also it would be interesting to see any specific aproaches in DB CRUD operations of Slim 4 in a comparison with Slim 3.

I did some REST API for my Android application with Slim 3. That’s nothing special. It simply gets REST requests and sends responses with JSON to show some items’ galleries in the Android app.

The tech stack for backend: Slim 3, vlucas/phpdotenv (for DB credetials), MySQL.

The backend implements container to set PDO mode. BUT Slim 4 docs say: “Slim no longer has a Container so you need to supply your own.”, “Slim uses an optional dependency container to prepare, manage, and inject application dependencies.”. And the question namely is - Should one implements container to have one’s codebase to be clear and project structure to be simple as much as it can be or rather not? With container, in some ways it’s convenient to have such files as dependencies.php and settings.php in src folder.

Thank you in advance.

Yes you still should use a container in Slim 4 as “compositon root”. Use dependency injection (constructor injection) and write simple classes with only one responsibility (SRP).

The most popular container (PSR-11) implementations are PHP-DI and league/container.


// Create container
$container = new Container();

// Set container to create App with on AppFactory
$app = AppFactory::create();

Thank you odan for your reply. I had similar thoughts, but needed their professional confirnmation.

1 Like