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.
54 episodes4 hrs 45 mins

Overview

Starting completely from scratch, build a modern PHP framework with all the features you’d expect.

Whether you’re new to PHP or not, this is a great exercise for learning what happens under the hood, arming you with knowledge you can apply anywhere you use PHP.

We’ll cover routing, controllers, views, the container, accessing the database, models, authentication, config, CSRF protection, exception handling, pagination, validation, flashing messages and much more.

Let’s dive in and build a PHP framework, step-by-step!

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!