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
18. Creating a controller

Episodes

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

Transcript

00:00
Although we've moved all of our routes over to a separate file, we're kind of back to the point where everything inside this file is going to get a little bit messy if we're registering our routes as closures. Obviously, what we have at the moment is incredibly straightforward.
00:15
We're just creating a response, writing to the response and returning a response. But as you can imagine, the more routes that you have in here and the more logic you add is very quickly going to make this routes file a mess. What we want to do is create out a controller. So let's go ahead and look at how we do this now.
00:35
And I'm going to get rid of this about route and we'll just do this for the homepage. OK, so first of all, where are our controllers going to live? Well, typically we'll have a directory in here called HTTP that deal directly with HTTP stuff like our controllers. And then inside of here, we'll have another directory which we will call controllers.
00:55
And then we're going to go ahead and create a controller out in here. So this is going to be a PHP class and we're just going to call this home controller. And then in here, we are going to create an invoke magic method to respond to this. There are a couple of other ways that you can do this, but I typically recommend just using invoke.
01:12
OK, so how do we use this home controller? Well, over in our web routes, because we're using league route rather than a closure just inside of here, we can just directly give the namespace to the controller that we want to reference. So we've now created this home controller and that should now respond to our homepage.
01:32
Let's see what happens. OK, so could not resolve a callable for this route. That is just because inside of our home controller, this is just a plain old class. Now, we don't have a namespace in here for some reason.
01:44
So let's go ahead and just fill this in now. And this is under app HTTP and controllers. And that might have been the cause of that error before, but either way, we don't have any way to handle this. So let's go over to our web routes and just make sure we import the namespace for that home controller.
02:04
OK, let's go over and give that a refresh now. And yeah, we're back to the same error. That is just because we're not doing anything to make this callable. So we're going to create an invoke magic method in here.
02:16
Let's just file dump here and we'll kill the page and we'll say home controller. And if we head over and give that a refresh, there we go. So this is now responding via a separate class with this controller. Now, we want to do exactly the same thing in here that we did within our route closure.
02:34
So the first step is to create our response. So we create our new response, remember, from the diopterous package. Then we go ahead and return that response. If we just did this on its own, we'd just get an empty response in here.
02:48
But we want to fill this in with some data. So response, get body, right. And we can just do exactly the same thing and write any basic HTML in here that we need. Let's go over and there we go.
03:03
So successfully now move that over to a controller, which makes things a lot easier. Now, how do we access our request within our controller? Let's make this into a more dynamic example by showing a query string value inside of here. So let's get rid of this.
03:21
And we just basically want to output anything in here that exists within the query string. Let's take a name, for example. So I'm going to add my name to a query string here. And I want that to display out here.
03:33
Well, inside of our controllers, when we use league route, this is going to automatically pass through our request into our controller for us and even into our closures. It will work in exactly the same way with closures as well. So let's go ahead and pull in our request in here.
03:51
And we could even type in this to our server request interface, but request is just fine. And let's go over and see the difference. Okay, so yeah, let's have a look here. So, oh, yeah, so we do need that to be a server request interface or a server request.
04:06
Not too bad. Give that a refresh and there we go. So now from our request object, which we looked at earlier, we know that we can do things like this. Request in here, and we can say get query string or query parameters.
04:23
And then we can access any of the query parameters that we want from here, for example, the name. So over here, now it will display the data from the request. And we can pretty much do the same thing with forms. So if we were posting through to a controller, our request would contain the parsed body.
04:43
So we have get parsed body, which will contain any of the form data we're posting through. Once we get things like authentication, when we look at our registration form, we'll parse data through to a controller, extract it out with the get parsed body function, and parse that through to register a user in the database.
05:01
Let's get rid of this for now because that's not too useful. But we now know that we have access to our request inside of here. So once again, I'm just going to write home here. And we're back to our routing working nicely, but now with controllers.
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!