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.

Episode summary

In this episode, we make working with our application container much easier by introducing a handy helper function for resolution, inspired by the Laravel framework.

Previously, every time we needed to get something out of the container, we had to call methods like getContainer()->getRequest(), which gets repetitive and clutters up our code. To clean this up, we create a new app() helper function. This function lets you grab anything from the container with a single line, like app('request') or app('view').

We walk through where this helper should live, how to implement it, and the little trick that makes it work even though helpers can't receive the container instance directly. That's where the container singleton comes in!

Once our helper is added, we refactor our service provider code to use it, which makes things cleaner and more readable. Finally, we check that everything still works as expected by visiting the homepage and confirming that pagination is working just as before.

This episode is a game-changer if you're looking to write tidier and more maintainable code.

Episode discussion

No comments, yet. Be the first!