Laravel Service Containers for dummies.
If you find yourself reading this piece, then I guess I don’t have to sell you on how awesome Taylor Otwell’s laravel is. 😍….
Service Providers and the Service Container are an essential part of the framework. Let’s dive into what they are, what they can do and how they can be used to build apps that are simply super easy to maintain.
Though laravel’s documentation is one of the best you’ll ever come across, it could be quite technical for someone just starting out. Let’s try and break down this core concept of the framework.
The Service Container:
The Laravel service container is a powerful tool for managing class dependencies and performing dependency injection. Dependency injection is a fancy phrase that essentially means this: class dependencies are “injected” into the class via the constructor or, in some cases, “setter” methods.
Simply put, the service container is a container
that holds classes you’d like to resolve(instantiate)
programmatically later in your application.
Service Provider:
Service providers are the central place of all Laravel application bootstrapping. Your own application, as well as all of Laravel's core services are bootstrapped via service providers.
In simple terms, a Service Provider is used to register
classes into the service container. Simple right ?? 😉.
This allows you to do cool things like Dependency Injection and Binding to interfaces.
How to Use them
Let’s say in our app, we want to send messages via serval channels eg. sms, email, push notification, slack etc… you get the idea.
Let’s create a class to send sms messages
next let’s create our service provider with the artisan command php artisan make:provider SmsServiceProvider
The register method is where we register our service with the service container. Basically we are saying hey laravel, take note of the SmsService class. I will need an instance of it later.
Super simple
Next add the service provider to the $providers array in config/app.php
and that’s that.
Let’s take a look at how it’s used.
On line 9, we want to get the name of the service to use by following our own naming convention by converting the $service
variable to the service class name using laravel’s studly_case helper function. convention over configuration
😉.
Line 11, is where we resolve
the service from the container. Suppose we pass sms
as the $service
to sendMessage()
, we’ll end up with SmsService
. We then tell laravel to make an instance of that class and call the send
method on it.
Here’s an overview of how the container resolves our class
With our current implementation, suppose we wanted to send messages to our mobile app via push notifications, all we’ll have to do is to follow the following steps
- create
PushNotificationService
that implements theMessageInterface
- create a
PushNotificationServiceProvider
and register it in the providers array. - pass
push_notification
as the$services
to thesendMessage()
for theMessageService
that’s it…. super simple
Notice that we were able to easily extend the functionality our message service without changing it’s current implementation? simply awesome….
This is just taste of what you can do with Service Providers in laravel.
Learn more on how to bind into the service container here
Happy coding my fellow artisan 💻.