Backend and Infrastructure

Services and Dependency Injection

This page is archived

We're keeping this page up as a courtesy to folks who may need to refer to old instructions. We don't plan to update this page.

Sprout Video

Note: This is a PHP-based introduction to dependency injection as a concept. It doesn't use a Drupal-specific example. Also, the library it does use, Pimple, is no longer maintained.

Our app is small now, but as it grows, the app.php file will get harder and harder to read. The best way to fix this is to separate each different chunk of functionality into different PHP classes and methods. Each of these classes is called a “service” and the whole idea is sometimes called Service-Oriented Architecture. In this tutorial you're going to create a new service, and learn about different ways to access external objects from a service. Then, we'll set up our app to use dependency injection to get the job done right.

Additional resources

Service-Oriented Architecture