‫All right guys.
‫So now we're going to do our first big refactor of the router.
‫Because what we have right now is extremely simple.
‫So our routes we basically just have a URL mapping to a file a controller.
‫We can't specify if it's a Get request, post put delete or anything like that.
‫So that's what I want to do next is add that functionality.
‫And in order to do that we're going to create a router class that has methods like get and post.
‫So ultimately we want to be able to create a new router instance.
‫So I'll just you guys don't have to type this.
‫But let's say we would say equals new router.
‫Right.
‫And then I want to be able to say router.
‫And I want it to be a Get request or handle a Get request to whatever listings.
‫And then if that listings is hit then we want to load controllers, slash listings, slash index dot
‫PHP.
‫So that's I want to be able to do this.
‫And I also want to be able to do post or put or delete.
‫So in order to do that we need to make a router class so that we can instantiate a router object.
‫All right.
‫And I just want to say before we start that I understand that it can be frustrating when we we write
‫some code.
‫It works.
‫You understand what's going on.
‫And then I say forget that we're going to do this.
‫I understand that, but there's a reason for that.
‫And that is so that you understand what's going on at a fundamental level, rather than just starting
‫out with the harder way of doing it, you know, the more difficult way of doing it, and you just copying
‫me and just writing the code, you know, that's easy.
‫The hard part is really understanding what's going on.
‫So I think making incremental changes and refactors is the best way to do that.
‫But I do get that frustration.
‫I get it myself when I take courses, but it is needed and there is a reason for it.
‫All right.
‫So enough with the the spiel, the ranting.
‫Let's jump into let's go into the public index.php.
‫Because ultimately this is where everything starts, right?
‫This is where we get the Uri that's being requested that we go to.
‫Right.
‫This is where it starts.
‫We also want to get the method in this case.
‫So let's start by adding a method variable here.
‫And we can get that using the server Superglobal.
‫And saying request underscore method.
‫In fact let's take a look at that with inspect.
‫So we'll look at the Uri and we'll look at the method.
‫So if I come.
‫What's going on here?
‫So if I come back over here and I reload the home page, we see slash for the Uri and we see get for
‫the method.
‫So that's all we need to know to to do our routing.
‫So if I go to slash listings then we get slash listings and get if I were to submit a form to slash
‫listings this would be post.
‫All right.
‫So we know that this is the this is where everything starts.
‫So ultimately those two things will be passed in to the main route method in our router class.
‫So let's let's start to create our router class.
‫So I'm going to go to router PHP.
‫And I'm going to rename it to uppercase R router because that's the convention when you're dealing with
‫a class.
‫And then I'm going to just get rid of this right.
‫And we're not going to be requiring routes in this file anymore.
‫We're going to require it in our index.php.
‫So we don't even need that.
‫And then we're going to create a class called router.
‫All right now.
‫The the routes array is now going to be a property in this class, and we can make that protected.
‫We don't need to access it outside of the class.
‫So we're going to make it protected, call it routes and set it to an empty array.
‫So essentially when we go to our routes file and we say router get whatever that, that URL that we
‫pass in and the method and the controller are all going to be, are all going to go into this array
‫as an associative array.
‫So let's start off by creating the for the four methods that represent the Http request which is get,
‫post, put and delete.
‫So we'll come down here and I'm just going to put some comments in here.
‫So this will add a get.
‫Request or let's say no, it will add a get root.
‫Yeah, add a jet route and then as far as params go, it's going to take in a string which is the Uri
‫and then the param.
‫Pram string.
‫And it's going to take in the controller.
‫Okay.
‫Because we need to know those three things the Uri, the controller and the method.
‫We're going to know the method because that's the name of this of this method.
‫And when I say method I mean the Http method.
‫And that Http method is the same name as this class method, if that makes sense.
‫As far as the return.
‫That's going to be void.
‫So let's say public function get.
‫And it's going to take in Uri and controller.
‫Okay.
‫Now, before I put anything in the body, I'm just going to copy this and we're going to add the other
‫three.
‫So let's paste this underneath.
‫This is going to be.
‫So both of these are going to change to post.
‫I have an extension that makes it.
‫So if you select more than one and you and you type something it stays the same case.
‫So just just make sure that if this is upper case make sure this doesn't go to upper case.
‫And by the way that extension is called this right here multiple cursor case preserve.
‫So it just preserves the case of your of your select more than one thing.
‫All right.
‫So let's add another one here.
‫This one is going to be put.
‫Another one and this is going to be delete okay.
‫So we have our three methods that will handle the three sorry four methods that will handle the Http
‫methods.
‫Now all of these are going to be very similar.
‫So I'm going to go into the get method here and just show you exactly what this is going to do.
‫It's going to add to the routes array.
‫So if we say this dot routes we're accessing this right here which starts off as an empty array.
‫But if we call route or get then we're adding to that.
‫So we're going to say equals.
‫And then we're going to set that to an associative array that has a method.
‫In this case, it's going to be get because we're calling the get class method.
‫And then the Uri, which is going to be whatever Uri is passed in.
‫And then we have the controller.
‫Which is going to be whatever controller is passed in.
‫And let's put a semicolon here.
‫All right.
‫So that's what we want this method to do.
‫Now the post and put and delete are going to do the same exact thing.
‫Except this will be different.
‫This will be post put and delete.
‫That's the only difference.
‫So instead of repeating ourselves and just putting this in all four, let's create a route at the top.
‫I'm sorry, a method at the top called Register route.
‫And then we can call that from the four methods below.
‫So let's say public function register route.
‫And that's going to take in three things the method, the Uri, and the controller.
‫All right.
‫And then what that's going to do is this.
‫So I'm going to cut this from here and I'm going to paste it in register route.
‫And all I'm going to do is replace this hardcoded get with the method that's passed in.
‫So now from the get method we can then just call this.
‫Register route.
‫And then we can pass in.
‫Get as the method.
‫Just pass in the controller.
‫Yeah the Uri.
‫So Uri and controller.
‫Okay.
‫So then all it's doing is this it's adding to the routes array.
‫And it's going to put in the get method.
‫It's going to put in whatever Uri is passed in from the route in the routes file and the controller
‫that's passed in.
‫And then all we have to do is that same thing in all of these.
‫So for post we're going to do the same thing, except we're going to change that to post.
‫For put.
‫We're going to just change this.
‫To put and then for delete.
‫Change that to delete.
‫So it's a bit cleaner than having all this in in every single one with just the method changed.
‫All right now.
‫This takes care of the the Http methods, but we don't have a way to actually load the controller,
‫right.
‫This this will create the the array item, but it's not doing anything with it.
‫So that's where the root method comes in.
‫That's going to go down at the bottom here.
‫So let's say this will route the request.
‫And let's say I'm going to take in string of Uri.
‫And it's also going to take in the method.
‫And it's going to return void.
‫All right.
‫So let's say public function.
‫Route.
‫Takes in the Uri.
‫Now this Uri in method is the actual Uri and method being called.
‫It's what is whatever is going to come from this.
‫This is what's going to be passed into this.
‫So what we have to do is loop through all of the routes.
‫Because if we add, let's say in our routes, we say router, get slash router, get listings, router
‫get listings, create router, post create, whatever.
‫Those are all going to be put into this array, this routes okay.
‫And they're going to be put in that through either get post whatever method is called.
‫So now we need to loop through that and see if it matches the actual Uri and method that is being called.
‫So this.
‫Okay.
‫So we can do that by doing a for each.
‫We want to loop through the routes.
‫So we need to say this since it's a property of the class this.
‫Roots, as we'll say.
‫Roots.
‫And then we just need to do a check.
‫Let's say if.
‫And we want to check the the current.
‫Uh.
‫I'm sorry.
‫This should be root.
‫Okay, because it's going to loop through and for each route or each iteration, this is the variable
‫we use.
‫So we're going to say if the current route that we're in the current iteration if the Uri.
‫If that matches the actual Uri that we're that we're accessing through the browser and the current root
‫or current iteration method.
‫Is equal to the actual method that we're getting from this right here.
‫Ultimately from the server.
‫Superglobal if that's true, then we want to load whatever the controller is for that particular row.
‫Because remember in that routes array there's the method the Uri and the controller.
‫Okay.
‫I'm sorry.
‫Not the method, but the.
‫Well, yes.
‫Yes the method.
‫So to load the controller, we're just going to require and let's say base path.
‫And we're going to pass in here the route and then get the controller file.
‫So that's going to be required.
‫And that's what we're going to see on the screen.
‫And then we just want to return.
‫All right.
‫Now if we try to load a route that isn't in the isn't in that array this routes.
‫So if it hasn't been added then we want to show the 404.
‫And we can handle that here.
‫Just go under the for each.
‫And let's first of all send a response code of 404.
‫And then let's load.
‫View.
‫And we'll load slash not slash.
‫Just error slash 404.
‫And we'll make this more dynamic because it could be something else.
‫But let's just do this for now.
‫So load 404 and then exit.
‫All right.
‫So now.
‫We should be able to add routes well before we add any routes.
‫Let's finish the index.php, because right now we're not calling this method this route.
‫We didn't even instantiate the router yet.
‫That's going to happen here in the index.php which is our entry point.
‫So first off let's change this to uppercase because we change that because it's now a class.
‫So we're requiring the router class.
‫Now let's instantiate a new router object.
‫So new router.
‫And then let's bring in the routes file.
‫So routes equals require.
‫Uh, whoops.
‫Wrong syntax.
‫It's NodeJS.
‫So base path and then.
‫Routes.
‫PHP.
‫Okay.
‫Once we do that, I'm actually going to bring.
‫Yeah.
‫Let's bring both of these.
‫Oops.
‫Let's bring both of these down.
‫To hear.
‫And then what we're going to do is call the root method.
‫And we want to pass in that Uri and method.
‫So let's say router.
‫Route.
‫Pass in the Uri and the method of whatever the page we're visiting.
‫Okay, so this method right here is.
‫This.
‫So whatever is passed in, that's what we're visiting.
‫It's going to loop through our roots in our array and match it.
‫If it matches, it'll load the controller which and then turn the controller will load the view, because
‫that's all the controllers are doing right now.
‫All right.
‫Now we don't have any routes set.
‫We can get rid of this actually I'll keep it there because just so we can see what routes we need to
‫create.
‫Although we don't need to create this anymore because it's handled within the router.
‫So let's take our router and we're able to access this variable because in our index PHP router is defined
‫above our routes file.
‫So make sure that you don't have this underneath it.
‫So now we can say router and call get.
‫And let's say for slash we want that to load the controller.
‫So, controllers, what is it?
‫Yeah.
‫Home dot PHP okay.
‫And then copy that down.
‫So for slash get slash listings then we want to load.
‫Slash listings, slash index dot PHP.
‫And then we also have.
‫Listings slash create.
‫And we want that to load controllers.
‫Listings slash create.
‫Dot php okay now we can get rid of this.
‫And this is to me is much cleaner than this.
‫And it lets us lets us specify a method.
‫All right.
‫So let's save that and let's see what happens here.
‫If we go to our home page first.
‫So we get cannot access offset type of type string on string router.
‫So let's see I think up here we have.
‫This right here.
‫So right here, what I'm doing is I'm just setting this roots to this every single time.
‫And what I want to do is add to the array.
‫So I need to have brackets here.
‫So that should fix that issue.
‫There we go.
‫So that loads the home page.
‫Let's go to slash listings.
‫We have a link right here that goes to it.
‫And we're seeing the right page because it's loading the right controller which is then loading the
‫right view.
‫Right.
‫So just in case you're still a little confused, we're matching the route and it's loading the correct
‫controller, which in this case is the listings controller because that's what we have here.
‫Listings matches this controller.
‫And in that controller so listings index we're loading the listings index view which is what we're looking
‫at.
‫All right.
‫And then if we go to post a job that loads the the listings.
‫Right here.
‫Create PHP loads that controller which then loads that view.
‫Now, one thing I want to do before we move on is in the router.
‫Instead of just having this down here, let's let's make a separate method for our error and also make
‫it so we could pass in a different code if we want instead of hard coding 404.
‫So let's go right above the root method and we'll add some comments here and say load.
‫Error page and there's no params, but we'll have a return of void and we'll call this let's say public
‫function error.
‫So we can just say this error and let's pass in.
‫Yeah let's let's actually have a param.
‫So param will be the Http.
‫So it'll be an I guess an int.
‫And it will be.
‫What do we call it?
‫Http we'll call it Http code.
‫So then down here we'll say Http code.
‫And let's set it to a default of 404.
‫Okay.
‫Now, what I'm going to do is take these three lines and cut those, and then we'll paste that in here.
‫And instead of the the hard coded 404, let's pass in Http code.
‫And then here where we're calling the view let's actually make this double quotes or concatenate whatever
‫you want to do.
‫And we want to put the code variable or Http code in there.
‫Okay.
‫So that way if we have like a 403, we can have a 403 view etcetera.
‫So now down here we can just call this.
‫Error and you could pass a code in, but it's 404 by default, so we'll leave it.
‫Now let's test it out.
‫We didn't even test that out yet.
‫So let's say.
‫Slash test.
‫And it loads the 404 view.
‫Now let's do this.
‫Let's add another view in error.
‫So we'll create a new file here called 403 which is it means unauthorized.
‫And we'll be using this when we get to authentication.
‫So if we create that and then we just copy the 404.
‫And pass that in and just change it to 403 error.
‫And we'll say.
‫You are not.
‫I'll say you are not authorized to view this page.
‫There's so many different ways you can do this too.
‫We could pass the data into the file, which I think we're going to end up doing at the end.
‫But just to show you, if I were to now go into the router and pass in 403 here and then reload, now
‫we see the 403.
‫Okay, so I'm just going to leave that empty, which is by default 404.
‫All right.
‫Cool.
‫So I know that this was kind of a long video.
‫There was a lot to it and nothing changed in the functionality.
‫That's that's one thing that I want you guys to realize about this particular course is it's not about
‫it's not really about the app.
‫Right?
‫It's not about the job listings.
‫It's pretty simple.
‫It's about structuring it with vanilla PHP, which a lot of people have issues with and have problems
‫with because it's so you have so much freedom.
‫So it's confusing to know which way to do it.
‫And there's a thousand ways to do this.
‫There's better ways.
‫There's much, much worse ways.
‫This is just one way.
‫And you might, you know, find things that you want to change around and that's fine.
‫But all in all, I think that it's a good educational experience, even if you're not going to be building
‫vanilla PHP applications.
‫And it'll also help you understand frameworks like Laravel better.
‫All right, guys, so that's it for this particular section I think we're going to stick with this router
‫for now.
‫We'll refactor later on.
‫We'll make it so that we'll have controller classes and stuff.
‫In the next video though, I want to start to get working with the database and, you know, fetching
‫jobs and being able to create jobs and so on.