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
15. Responding in routes

Episodes

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

Transcript

00:00
Now we registered this get route in the last episode, but we didn't extract a response out of this, or we didn't dispatch our router to dispatch what we get from the router, we're going to create out a response variable in here. And we're going to access our router and we're going to call
00:16
the dispatch method on this. Now let's just recap. We created a request earlier in our service provider. We went ahead and registered our router to dispatch a route.
00:28
We need our request because our request contains information about the current path and anything within that. So in here, what we want to do is pass in our request. So again, this is looking a little bit messy.
00:42
We're going to change this up later, but for now, let's just grab out of our container, our request and pass that directly into dispatch, let's go over and see if anything has changed here. And you can see that this has now actually technically responded
00:58
to the route we created earlier. But this is not what we want to do. Ideally, what we want to have is the ability to emit this response. So inside of here, we can add anything we want to the response, like an HTML body.
01:12
If for example, at the moment, we just returned something like home, this isn't going to work. Give that a refresh and we get some error about the response must be a particular implementation of an interface.
01:24
Now, the request that we pulled in earlier comes from that directorious package, which also includes a response object as well. So you can see that we've got this response object just here, but what we want to do is emit a response.
01:40
So let's go and pull in the HTTP handler runner package from the same author as this and see how we can do this. And once we're done with this, if any of it's confusing, we're going to run through it again.
01:53
So don't worry. Okay. I'm going to go ahead and pull this package in and inside of here, we have what's called an emitter.
02:00
So what we're going to do is take this response that we have here. And down here, we're going to new up an SAPI emitter, and then we're going to emit the response to the client. So let's go over and see if this has made any difference at all.
02:17
And yeah, sure enough, it hasn't just yet. Now, the reason that this isn't working is because we're returning a plain string from our route. What we instead want to do is create out a response directly in here.
02:30
So we want to either new up a response or grab it in a slightly different way, which we'll look at later. And then we want to return that response. So let's just do this here.
02:40
Pretty plain, but let's do this and make sure we pull the namespace in from directories and response, and let's give that a refresh and there we go. So we've got now a proper response being sent to the client, but with nothing in it.
02:55
So how do we write to this response to actually output something on the page rather than just far dump something? Well, we take the response, we grab the body and we write to the body. So as an example, let's just write home.
03:09
So a little bit different from just returning home and a little bit more complex, but nothing too bad. Let's give this a refresh and there we go. So we now have a proper response being sent to the client by
03:22
newing up a response, returning it from this route. We get the response directly in here, and then we emit that to the client. So with this in mind, what we can now do is actually emit out HTML to the client. We're not going to be doing this because we're going to be using views instead,
03:40
but as you can see now we're actually getting proper HTML sent through. So eventually what we're going to do here is write a rendered template from a view using Twig, and that will give us back a full HTML page. Now this is all well and good, but we're doing again, everything
03:58
inside of our bootstrap app file. So let's take a few minutes in the next episode to convert this over to make the dispatching of everything a little bit more convenient, and then we'll look at registering our routes in a slightly better way as well.
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!