Playing
06. Adding service providers

Episodes

0%
Your progress
  • Total: 4h 45m
  • Played: 0m
  • Remaining: 4h 45m
Join or sign in to track your progress

Transcript

00:00
To demonstrate how service providers work, let's take the simple example that we've created here
00:04
and move this over to a service provider rather than doing everything in line within our bootstrap file. So the first thing we need to do is figure out where these are going to live. I'm just going to create out a directory here called providers and inside of here I'm going to create out a general app service provider. When we get to adding other functionality we'll create separate
00:26
service providers and you can create as many of these as you need. So we're going to create out a php class called app service provider and let's go over to this and look at what we need to do. So the first thing that we need to do with our app service provider is extend the abstract service provider from leak container so it can use it and then we need to go ahead and implement
00:48
a specific interface depending on the kind of service provider we're creating. Now we have two options here. We have a standard service provider and we have a bootable service provider. Throughout the course we're only going to be using bootable service providers and this means that we have a boot method which we can use to just run some code that we need to happen at runtime and
01:11
we have a register method which we can use to register stuff. So let's go ahead and pull in the bootable service provider interface and again these are from these namespaces just here. Now with this we need to implement the methods that this requires. So the two methods that we need in here are a boot method so let's create this out now. You can auto complete that with your editor as
01:34
well if you need to. This doesn't return anything it just runs a bunch of code here so we'll add something to this in a minute and the next one is a register method which we can use to register things on our container. So this looks like this it's just register and again that returns nothing here just responsible for registering stuff out. The last thing that we need in here is a provide
01:56
method so let's go ahead and stop this out here and what this is responsible for doing is checking whether when you try and access something from the container this service provider does actually return something. So we'll look at that in a second but let's first of all look at registering something inside of here. Okay so let's come up to our boot first of all and just go ahead and var dump
02:20
booted just to see what this looks like and then we'll go ahead and add this service provider to our container. So we're going to get rid of everything that we've done here and we're going to say container add service provider and we're going to new up the app service provider that we have just created and if we take a look in here when I created this within
02:39
my editor it automatically added the namespace. You're just going to want to make sure that you add that in there manually if not. Okay so now that we've added this service provider let's go and check out what happens. Give that a refresh and there we go booted. So the boot method is used to do anything at runtime that we need to do like set something up or call some sort of
03:00
manual action. For example what we could do is we could take ignition and we could move that over to our app service provider. Now we're going to do that but a little bit later we want to make sure that we only do this only do this when debug is enabled. So we'll pop that comment in there for a little bit later. So now that's gone ahead and registered ignition within our global app service
03:27
provider and this is the perfect place to do this. Okay so we're really interested in this register method we want to pretty much just do what we did over in our bootstrap app file inside of here. So how do we do this? Well we want to go ahead and grab an instance of the container to rather than use that container variable we now want to go ahead and add that name create out a closure in here
03:52
and now everything's nicely tucked away and then do exactly the same thing like this. So that's just the alternative to doing that directly on the container. Okay let's go over to app here and let's go and try and grab this out of our container. So let's say container get name. Okay let's head over and you'll see we get this error return value of provides must be of type bull and none returned.
04:18
So we didn't implement the provides method that we implemented earlier. Now this is responsible for making sure that what we try and access out of the container actually exists. So typically what this would look like is defining out the services that are being returned here in our case name. So we can say name and then we want to just check if it's in an array. So we'll get the name of the
04:40
thing within this id argument and we just want to check it exists within our services. This is just something that we have to do. So we're going to just use in array here to check if the id of the thing we're trying to request exists within what we've defined here. So that should do the trick. If we head over and give that a refresh now, sure enough you can see that this works. So we'll just
05:02
have to do this for every single time we create a new service provider and every single service that we register will need to go within that array. Okay so that is service providers in a nutshell. We're going to be registering a few of these but we're going to be doing this later via our config so we don't have to manually add them to our bootstrap file.

Episode summary

In this episode, we're taking a look at how to use service providers to clean up and organize our application setup. Instead of cramming everything into our bootstrap file, we're extracting things out into dedicated service provider classes.

We start by creating a providers directory and then set up an AppServiceProvider class. You'll see how a service provider can extend the abstract base from our container and implement the BootableServiceProvider interface, giving us the handy boot and register methods. We chat about what those methods are typically used for—register for wiring up services in the container, and boot for things you want to run at runtime, like side effects or manual setup steps.

Next, we update our bootstrap logic to actually add/register this service provider. You'll see a quick test where we dump out some info in boot just to prove it's being hit. We also do a quick aside showing how you might migrate things like Ignition (for debugging) to your service provider, but with a note to make it conditional in the future.

You'll see the practical difference between directly registering services in the container versus doing so inside a provider—it's all about keeping things nicely tucked away and organized. We hit a snag with an error when trying to fetch our service, and that leads us to implement the provides method, which is needed to tell the container what this provider actually offers.

By the end, you've got a clear, working example of a tidy service provider and the foundation for using more of them in the future—eventually through configuration rather than messy manual registration.

Episode discussion

No comments, yet. Be the first!