Handling Dependency Injection in Slim Framework 4

Hello Slim community, :smiling_face_with_three_hearts:

I am working on a project using Slim Framework 4 and I’m trying to get a better handle on dependency injection. I understand the basics, but I’m unsure about the best way to manage dependencies as my project grows.

I also check this: https://discourse.slimframework.com/t/how-to-select-multiple-id-in-mysql-using-slim-frameqlik

Can anyone share strategies for organizing and managing dependencies effectively in a larger Slim 4 application? Also, are there any recommended practices or tools that can help streamline this process?

Thanks for your help!

Best regards, :innocent:

Hi Tiara

As a quick overview, I would like to share some strategies and best practices for organizing and managing dependencies in a larger Slim 4 application:

  1. The most important tip: Use a dependency injection container. I would recommend PHI-DI.
composer require php-di/php-di
  1. Learn how autowiring works. Autowiring will automatically inject dependencies based on type hints. PHP-DI supports autowiring by default.

  2. Learn how to define custom DI container definitions.

  3. As your application grows, it’s important to organize your services and their dependencies in a structured way.

I explain that in my blog post. See here: Sim 4 Tutorial

I would also recommend checking existing Slim 4 skeleton projects for inspiration and best practices. You can customize these to fit your project requirements.