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
52. Container resolution helper

Episodes

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

Transcript

00:00
Let's register a container resolution function,
00:03
which will allow us to grab any item out of our container really conveniently. Again, this is inspired by the Laravel framework. Let's go ahead and build it now.
00:13
So I'm going to get rid of this from the home controller. And we're just going to pick somewhere where we're using our container. And we're going to swap this out.
00:21
So let's go over to our app service provider. And we know that in here, we are repeating ourselves here with this getContainer, getRequest, this getContainer, getRequest, all the way down here
00:33
for all of these that we need. It would be really cool if we could do something like this. So we might want to just swap this one out. We might just want to say request.
00:43
So it comes from our app. We want to resolve this out of the container with this really helpful short function. OK, so let's go ahead and build this out.
00:52
So we'll go over to our helpers. Let's go and create this as app. And of course, we need to know what we're actually resolving out of the container.
01:01
And that is just going to be an abstract. So that will be the abstract thing that we're trying to get out. And we will return the concrete implementation of whatever
01:10
we need from our container. Now, we already know how to grab stuff from our container. Because we're working in a helpers file here, we can't access our container directly.
01:20
We can't pass anything into this file. So that's why we created the container as a singleton. So we can just grab an instance of it wherever we're using it at runtime.
01:32
So we're just going to return getInstance. And then we'll grab the abstract. And that will return to us the actual thing that we need. Simple as that, but now that's going
01:41
to make a huge difference to how you resolve things out of your container. Let's go over to our app service provider. And let's change this over to app and request.
01:52
Let's change this over to app and request. And we'll change this over to app and view. And I'm sure you'll agree, that's a lot tidier. OK, we'll do the last time for here.
02:03
And we should be good. Feel free to change over anywhere in the application now that we're resolving things out of the container. I'll just leave these as examples.
02:13
OK, if we head over to our home page, yeah, sure enough, our pagination still works. It still resolves the request out of the container to be able to build this up.
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!