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
16. Cleaning up responding

Episodes

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

Transcript

00:00
We created this app class for a very good reason. We want this to handle the entire high level life cycle of our application, rather than doing everything line by line inside of our bootstrap file.
00:13
So what we're going to do in this episode is make use of app to actually resolve out our request, our router, and then emit a response when we call run. So inside of our app, what do we need to get this working? Well, we know that we need a request.
00:29
We know that we need our router. And then in run, we can use our router to dispatch this request and then emit the response. So let's start to create out some proxies in here that we might need.
00:41
So we've got a server request in here, which will be using the server request interface, the PSR seven server request interface, so we can type in that with request, and we're also going to need our router, which we know is just league route and router.
00:57
So let's pull that in as well. So when we go ahead and construct this app class, we can either pass these manually in, or we can just pass our container in and resolve these out of the container.
01:10
So let's go ahead and pass in our container. And this is using a container interface again with PSR. So we can just pull that in like this, and then we can just go ahead and assign request and router to the things that we've stored in our container.
01:26
So let's go and say this request and assign that to the container that we're going to eventually pass in and we'll get our request out in here and we'll do the same thing for our router as well, which we also have in our container on the router. Okay.
01:45
So now that we've done this, we can just change this up a little bit to get rid of our router here, because we're not going to need that. We can pass our container into app. So our request and our router can be resolved.
01:58
Now we don't have our router available anymore. So what we could do inside of app is create a helper method just to grab our router out of here. So let's create a get router and let's return this router from here.
02:13
And we can even type in that because we know that's going to return a router. Let's go back to bootstrap and app. And here we can say app get router. And then we can start to register any routes on here
02:26
directly from the router instance. So now down to this point here, where we grab the response and emit it. This is what we want to do inside of app run. So let's just paste this directly into here.
02:40
And we know that this is now this router and this request. So let's pass that directly into here and then we emit the response. And we can even tidy this up by getting rid of this temporary response variable. And we can put this directly into what we're emitting.
02:58
Okay. So just to recap, instead of doing the request and router stuff inside of bootstrap and app, we're now resolving these out of the container within our core app file.
03:10
We're then going ahead and running this, which will emit exactly like what we were doing inside of bootstrap and app. And we just created this helper here so we can extract the instance of the router out of our app class.
03:23
And this is what we're going to tidy up in the next episode to move this over to a root file. Okay. If we've done everything correctly and we give this a refresh, we should see
03:31
exactly the same thing that we've done inside of here, and that has now nicely tidied that up. Now, one thing that we will come across if we come down to our run method here is that this might error.
03:43
So we might get some sort of error on dispatching our root. For example, if we try and access a page that doesn't exist like this, we get a not found error. What we eventually want to do is handle these errors.
03:56
So let's slightly restructure this now. Later, we're going to look at things like custom 404s, which we'll do within here with a specific exception handler. So let's actually take this out of here and let's create the
04:09
response directly out of here again. Okay. So what we can do is emit the response, but we want to check if this response gets an error or not.
04:18
To do this, we can just do a try catch on it and we can catch any exceptions that are thrown. So let's grab a throwable in here. That's going to be an exception.
04:29
And then we'll just do something with that if that happens. So at the moment, this should work as normal. What we want to do up here, because we are potentially not getting a response when we emit this, we'll want to go ahead and
04:42
manually create out a response up here. So we'll always respond with a response, even if it's empty, but we're grabbing the response from our router here. And then potentially decorating this response if there's an error, but you
04:57
can see now what is happening is for this 404, we're just doing nothing. We're just emitting an empty response. For now, let's just re throw this error just so it gives us something. And then it's at that point here that we're going to decorate this
05:14
with a custom 404 or any other error that we come across. Okay. So if we go back to our homepage, this should all be working as normal. If this is a little bit fuzzy, that's fine.
05:25
Let's go over this one more time. Okay. So over in bootstrap and app, we know that when we create our app, we pass in our container.
05:33
We've already covered our request, which is then extracted out within our app. We need to pass the request through to our router, which we also pluck out of our container. And we pass that in at this point here.
05:47
Now it's at this point that the router takes in our PSR seven request. It works out what route we're trying to access. So in our case, it's just forward slash for the homepage and it goes ahead and dispatches it.
06:02
That matches up with any of the routes that we've added to our router in this case, forward slash. And then we create our response so we can add to the response. And in our case, we're just writing some really simple HTML.
06:17
Now the response will get returned from this route, which will then end up in this variable just here. And then we'll go ahead and emit that response to the client. And in this case, it's just a browser.
06:29
So all of this comes together with our request at the very top being passed in out into our router in the middle. So it can work out first of all, where this needs to go, but also our route now has access to any of the request data that we need, and then finally, we
06:46
finish up with a response, which sends that data over to the client. So again, don't worry too much if this doesn't make sense right now, as we go through, particularly if we start to look at controllers, this should start to come together.
06:59
But we now have a much better life cycle in our app and we have everything nicely tucked away. But we don't want to register all of our routes inside of bootstrap and app. So let's look at a slightly different way to do this.
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!