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.