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
13. Everything starts with requests

Episodes

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

Transcript

00:00
Everything in our app starts out with a request, which we will then use to resolve which route we want to display. We'll either use a controller or a closure for our routes, which
00:13
we'll get to a little bit later. And then when we run our app, we will put forward a response back to the browser. So we'll render out a response, whether that's just text or HTML. We'll get to responses a little bit later, but the first thing we want
00:27
to focus on is our request, and then we will see how this fits into the rest of the life cycle of our app. Okay. So to achieve this, we're going to go ahead and use a package, which will
00:38
allow us to have a PSR HTTP implementation of a request, which we'll again, come to a little bit later. This just means that it's a standard. This is going to work with our router really nicely.
00:52
And then we'll have the same thing for our responses as well. We want everything to work nicely together. Let's go ahead and just pull this package in and then let's see what it looks like. So let's go down to the section just here.
01:03
You can see the PHP package containing implementations of PSR 7 HTTP messaging interfaces and message factory interfaces. The factory part is what we want. Okay.
01:15
So we're going to go ahead and pull this in. So let's go over to the documentation and let's grab the composer command to go ahead and install this and we'll see what this looks like, and I'll show you a couple of examples as well.
01:26
Okay. So now that we've got this in here, we can either create this manually directly within our app, or we could register this within a service provider. I'm going to go ahead and register this in a service provider, because
01:37
we might need to manually access this somewhere else in our app a little bit later on, so again, let's grab one of our service providers and we'll create out a new service provider in here. So let's create out a request service provider and let's paste this in.
01:56
Let's go ahead and get rid of the content in boot because we're not going to need that. And of course, let's change the name over here to request service provider. Now we set up our config earlier.
02:07
So it's incredibly easy to just go ahead and go over to config and app and pull this service provider directly into here. So we'll pull in our request service provider and that's already registered for us. Okay.
02:22
So over in our request service provider, we want to bind this to the container. So let's say get container. Let's add in to the container using this request object, which comes from this package just here.
02:36
So let's go ahead and register that as the name, create out the closure in here, and we'll do this in just a second. We're not going to forget to go over to provides and pull in request class. So our container knows what we're resolving.
02:50
Okay. So this is really easy. We're going to use a factory to create a request instance. So we're going to pull in the server request factory again, from that package.
03:01
And we're going to create this from globals. There are a bunch of ways that we can create a request, but we're going to do this from PHP globals. So into here, we want to pass in the server globals from PHP, the
03:13
get and the post globals from PHP. We also might need a little bit later cookies. So let's pull in our cookie, super global array, and also our files, super global array, if we ever need them.
03:29
So now when we resolve this out, it's going to go ahead and create a request for us. Now, something else we didn't look at a little bit earlier was using shared. So setting this as shared means that every time we access this, we're going to get the same instance every single time.
03:45
We can even do that to our config service provider as well. So where we registered this, we can set this as shared. It just means that if we access this more than once, it's not going to keep resolving this and it's kind of acting like a singleton.
03:59
Okay. So we've got our request class inside of here. Let's go ahead and grab this out of our container and just see what this looks like. So up here again, let's kill the page and let's go ahead and access
04:09
this directly from our container. So we'll say get request from this package and let's just var dump on this and see what this gives us. Okay.
04:19
Let's go to the browser and we'll give that a refresh. And there we go. So we've now got a server request. Now, just a little bit of background.
04:27
If we open up our server request class or the server request class from this package, you can see that this implements, if we come down here, this server request interface. And this server request interface is from the PSR package. So it implements this interface.
04:45
And what this means is that when we use something like our router, which we're going to pull in later, this already expects this to be this PSR 7 request. So it will have all of the methods inside of here. If we just take a look and these are all named the same, this is just a standard.
05:02
It means that we can use any package that implements this kind of request and it always will contain exactly the same methods. So for example, we're going to be using get parsed body quite a lot, which will give us all of the data from the body.
05:17
When we post a form, we've also got something like get query params, which will give us all of the pretty obviously query parameters that we have in our application. So if we close this off, what we can do with this is grab out them query params.
05:34
So let's say get query params, and let's see what this gives us. Give that a refresh. And of course, at the moment, we've just got an empty array now, because over in our request service provider, we passed in all the details about our server and
05:52
the current request, what we can do is just add on to the end of here, some sort of example, for example, we're going to be implementing pagination later. So we'll need to know within the query string, what page we're currently on. Let's go ahead and hit enter on here.
06:06
And as you can see that request knows about the request that we've just put forward through PHP, and it contains the page that we're currently on. The same thing is going to happen when we eventually submit a form and we're not using a query string.
06:19
Instead, we're going to be sending a post request through to our server. The get parsed body method will contain all of the data about what's being sent through in that form. So don't worry if this doesn't make too much sense at the moment.
06:35
Once we get into our routing and our controllers, we'll see that we'll get this request through to each of our controllers automatically. And from that within our controllers, we can read out any data we need that's been posted through that's in the query string or any files that have been uploaded.
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!