Playing
05. Using a container

Episodes

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

Transcript

00:00
A crucial part of building out a framework is having a container where we can register services or pretty much anything we want into this container and then get them out at any point in our application.
00:11
Now that's a really simplified view of a container. We're going to look at how we do this at the very basics now, and then we're going to move over to looking at service providers, which offer a little bit more organization, and then we're going to go on to look at auto wiring. So there's a lot to cover here, but once we've done this, anytime we want any kind of service in our application, like cross-site request forgery protection or database support, we can add this to our container and get it out whenever we need to. Okay.
00:39
So what are we going to use for our container? Well, let's go over to league container. This is just one of many containers that we can use, and we're going to go ahead and set this up now, and I'm going to show you how it works. So let's come over to the current version of this.
00:53
Let's go over to the installation steps. And of course we need to pull this in with compoter. So let's go back over to our app and pull this in first of all. Okay.
01:03
Now that's pulled in. We know that we can access that directly in here because we're auto loading everything in. Let's go ahead and new this up and assign it to a variable. So let's create out a new container, which is under league and container.
01:16
And of course, make sure we pull the namespace in at the top here. Okay. Let's go and just bar dump on this container. And see what we've got.
01:25
Okay. Let's come over to our app and there we go. Okay. So we've got this container now with this, think of this as just like a box that we can add things into it, and then we can get them out when we need them.
01:37
So how do we add something to our container? Well, we say container add, we give it a name or an interface or some kind of class name here. Let's just use an example here of name. And then we want to return something from this.
01:51
So we can either do that via a service provider, which we'll look at later, or we can just add this in here within a closure. So I'm going to go ahead and return my name in here. And that has now added an item called name to the container that gives us back this thing. Now, in reality, we wouldn't do something like this.
02:10
We would return a full implementation of something we needed. For example, when we look at databases, we'll have some sort of database manager that will be returned via this, which we can use to access the database. Okay, so now that we've done this, how do we get something out of the container? Well, let's kill the page here and let's go and fire dump on grabbing this out of the container.
02:30
So let's say container get, and then of course, we just need to provide the ID of the thing inside of the container, which in our case is name. Okay, if we come over and give that refresh, there we go. So we've successfully put something into the container, retrieved something from the container, and that is the very basics of using a container. But we can do a little bit better than this.
02:52
Imagine that we add a bunch of services like cross-site request forgery protection, our database stuff, our views for when we eventually get to rendering views. Do we want to do this like this inside of our bootstrap file? What we could do, or we could break this out into separate files, but it makes sense to use a service provider instead of doing this in line within your code. Let's go over and see how we do that next.

Episode summary

In this episode, we're diving into what a container is and how we use one in our PHP framework. We start by introducing the concept: think of a container as a kind of box where you can register (store) services or objects, and then grab them out whenever you need them elsewhere in your application. It's a powerful way to manage dependencies and organize your app.

First, we choose the league/container package, a commonly used container in PHP, and show how to install it with Composer. Once it's in, we do a quick check to make sure everything's working by creating a new container and dumping its contents.

Then, we see how to add an item (in the example, just a simple name string) to the container and how to get it back out. It's a basic demonstration, but it sets up the essential idea: instead of scattering your objects or services throughout your code, you use the container to keep things organized and accessible from anywhere.

We wrap up by thinking ahead: as your app grows, you’ll probably be adding more and more services (like CSRF protection, database access, and views). Doing all this setup code inline isn’t great, so next up, we tease out the idea of using service providers for a much neater, more scalable way to handle all your services. Stay tuned for that!

Episode discussion

No comments, yet. Be the first!