This episode is for members only

Sign up to access "Build Your Own PHP Framework" right now.

Get started
Already a member? Sign in to continue
Playing
11. Registering service providers through config

Episodes

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

Transcript

00:00
Going forward, any new service providers that we create, we want to be able to conveniently add these to config rather than having to come here and add new
00:07
lines to add a new service provider. You don't have to do this, but it kind of makes sense to add your service providers to config, so it's much easier to register them. So what we're going to do is we're going to move our app service
00:19
provider over to configuration. Let's figure out how we want this to look first of all. So over in our app config file, let's create a list of providers in here. And rather than new these up in here, let's just give the name to the service
00:34
provider, let's say app service provider class, later on, when we get to creating any more, we can just add them in here and we are good to go. Okay. So over in our bootstrap and app file, what do we now need to change?
00:45
Well, we can get rid of this and instead, now that we've registered just our config service provider in here, we can go and grab that and just iterate over it. So let's pull out our config from our container. So let's say container, get and config class.
01:03
And then we're going to iterate over each of the providers from that app config. So we'll just need to say config, get app providers, and we'll say as provider. Now, what we can do is we can use our container to add a service provider. And we want a new app, the fully qualified namespace of the provider
01:24
that we're iterating over. So remember, we just have our app service provider in here at the moment. So that will register our app service provider, but because we're iterating over this list, any new providers we add will be added to, okay, let's go over
01:37
and give this a refresh and everything looks like it's working. We are just dumping this out here, but let's go ahead and just return back to where we are now, and we'll go over to our app service provider and we'll just do something in boot.
01:50
So we know that we've got ignition in here, but just so we know that this is being registered, let's var dump app. And there we go. Great.
01:58
We've got that registered. So there we go. We've now got a really convenient way when we create a new service provider to just add it to this list and it will be automatically registered for us.

Episode overview

In this episode, we make registering new service providers way easier by moving their registration into our config files. Instead of diving into the codebase every time we want to add a new provider, now we just drop its class into a list inside our app config.

Here's how we did it: First, we created a providers array in our app config file, where we simply specify the class names of any service providers. Then, in our bootstrap logic, we loop over this list and automatically register each provider with our container. That means whenever we need to add more later, it’s as simple as editing the config file—no more touching the main registration logic!

We tested the new setup to confirm everything still works, and now our code is both cleaner and easier to maintain. Next time we need a new provider, it's just a matter of adding it to config. Super convenient!

Episode discussion

No comments, yet. Be the first!