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.
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!