# Handling Dependency Injection in Slim Framework 4

**URL:** <https://discourse.slimframework.com/t/handling-dependency-injection-in-slim-framework-4/5930>\
**Category:** Questions\
**Created:** [June 27, 2024, 4:27pm UTC](https://discourse.slimframework.com/t/handling-dependency-injection-in-slim-framework-4/5930 "2024-06-27T16:27:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tiara](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tiara/32/1635_2.png) [@tiara](https://discourse.slimframework.com/u/tiara)\
**Post date:** [June 27, 2024, 4:27pm UTC](https://discourse.slimframework.com/t/handling-dependency-injection-in-slim-framework-4/5930/1 "2024-06-27T16:27:31Z")

</div>

Hello Slim community, 🥰

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: h[ttps://discourse.slimframework.com/t/how-to-select-multiple-id-in-mysql-using-slim-frame](https://discourse.slimframework.com/t/how-to-select-multiple-id-in-mysql-using-slim-framework/874)[qlik](https://www.igmguru.com/data-science-bi/qlikview-developer-training/)

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, 😇

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [June 30, 2024, 9:17am UTC](https://discourse.slimframework.com/t/handling-dependency-injection-in-slim-framework-4/5930/2 "2024-06-30T09:17:16Z")

</div>

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.

```auto
composer require php-di/php-di

```

1. Learn how autowiring works. Autowiring will automatically inject dependencies based on type hints. PHP-DI supports [autowiring](https://php-di.org/doc/autowiring.html) 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](https://odan.github.io/2019/11/05/slim4-tutorial.html)

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

- [GitHub - odan/slim4-skeleton: A Slim 4 Skeleton](https://github.com/odan/slim4-skeleton)
- [GitHub - slimphp/Slim-Skeleton: Slim Framework 4 Skeleton Application](https://github.com/slimphp/Slim-Skeleton)
