‫All right guys.
‫So now we're going to be refactoring our router because we're going to change it so that our controllers
‫are not just single files like this.
‫We want to have a listings controller or a listing controller class with methods that we can then basically
‫point to.
‫We can point a Uri to a controller method rather than a whole file.
‫So just to show you what that would look like here, we would say router and then whatever the method
‫and then the route will start with the home controller.
‫So let's just add that here.
‫So slash and instead of doing this controllers home PHP what I want to be able to do is this home controller
‫at and then index would be the method okay.
‫So we have to change the router so that it can handle this.
‫So I'll keep this here just for now.
‫And let's go into the router dot PHP.
‫And we're going to change this register route.
‫Right.
‫Because what we're doing now is not passing in a full controller path of a file.
‫We're passing this in which I'm going to call an action.
‫So let's change this to action.
‫And then we want to just change this to action as well.
‫And what we can do is extract the controller, which is this right here.
‫And we can extract the controller method which is this here.
‫We can do that first by turning this into an array where this would be the first item.
‫This would be the second.
‫So we'll come down here and let's say.
‫Trying to think of how I want to do this, because I want to show you guys what that looks like.
‫I'll put it in a variable for now.
‫So I'll just say array.
‫And then we're going to use explode because we're taking a string and we want to explode it into an
‫array.
‫So we want to separate it by the At symbol.
‫And we're going to pass in action, which is the string that's being passed in.
‫I don't know why I put const there.
‫I was just doing JavaScript code.
‫So and then I just want to show that.
‫So let's do inspect.
‫And die and we'll pass in that.
‫Okay.
‫And then I'm going to go to the home page and reload.
‫And you can see what I get is well actually.
‫I get that because it's looking at this right now.
‫So let's move this to the top.
‫And then reload.
‫Okay, so I have an array where the first item is home controller and the second is index which is the
‫method.
‫So let's extract that.
‫We can do that using the list function which if you're coming from JavaScript it's kind of like destructuring.
‫So basically what we can do is say list.
‫And then whatever I want to call the two variables the variable for this I'm sorry.
‫Not that the variable for this.
‫And the variable for this.
‫So for this let's say controller.
‫So in here we're going to say list controller.
‫And then the next one we'll say controller.
‫Uh, controller method.
‫All right.
‫Now just to check that.
‫Let's do and we'll do a regular we'll do inspect and die and let's check out what controller now is.
‫So I'll refresh and we see home controller.
‫Right.
‫If we look at the controller method and we refresh we see index.
‫So now we have the controller and controller method.
‫So as far as adding to the routes array this is still fine right.
‫We're adding the controller as the home controller.
‫But let's also add the controller method.
‫So this here controller method.
‫So now that will get added to the routes array.
‫So that's kind of the first part of what we need to do.
‫Now we need to handle the actual routing request.
‫This will add what we need to the router I'm sorry to the the array.
‫And then let's go down here and let's edit or refactor this route method.
‫So we're still looping through the routes.
‫We're still going to check the current Uri and method.
‫But right here instead of just requiring a file we're going to change this up a little bit.
‫So first thing we want to do is extract let's say extract controller and controller method.
‫So we'll have a variable.
‫Of controller and we're going to set that to.
‫Now we're going to use the name space here.
‫As well.
‫So it'll be app.
‫So app and then double backslash controllers and then double backslash.
‫So what this app controllers is if we look in actually we didn't create the listing controller yet.
‫When we do we're going to give it a namespace of app slash controllers.
‫So we're just adding that here.
‫Then we want the actual file which comes from root comes from that array.
‫Right.
‫It comes from this.
‫So let's add that we'll concatenate onto this.
‫Route.
‫And then.
‫Controller.
‫Let's close that up.
‫So that will give us the controller.
‫Now for the method let's say controller method.
‫And we can simply get that from the array.
‫So route.
‫Controller method, which we added above.
‫All right.
‫Now the controller is going to be a class.
‫So of course we have to instantiate that class.
‫So let's say instantiate.
‫Uh, instantiate class or let's say instantiate the.
‫Controller and call.
‫The method.
‫So for that, let's take the controller.
‫We'll call this this controller instance just to avoid confusion.
‫So instance and we can set that to new and then controller.
‫Okay.
‫Whether that's listings users whatever that controller is.
‫And that's why this is part of the framework part of our application.
‫Because it's not specific to listings or any resource.
‫We could use this with any any type of resource.
‫So now let's take that controller instance.
‫And we want to then call that controller method.
‫Okay.
‫So controller method like that and then we'll return.
‫So let's say we're dealing with the listings controller.
‫Then this this controller instance would equal new.
‫Listings controller.
‫Right.
‫And then here when you're calling controller instance controller method, if let's say we're on the
‫index, then it would be the listing controller or whatever you put a variable in like let's say.
‫Listing.
‫You don't type this out.
‫I'm just showing you what this actually does.
‫If we were visiting the listing index route.
‫So it would be like that, and then we'd call listing and then index, which would be the current method.
‫And then whatever you want to do for that specific route for the listing index that would go in that
‫method in the listing controller class, which we're going to create now.
‫So let's get rid of that.
‫And right now, if I were to reload, we get a bunch of errors.
‫So let me explain what's happening here.
‫These undefined array key one.
‫These are we're getting those errors because of these right here.
‫So it's basically looking for this type of string.
‫So a controller at method which these don't have.
‫So we're getting these errors right.
‫If I were to comment these out and reload those go away.
‫This one is now because we're looking at this route.
‫It's looking for home controller which doesn't exist.
‫So let's create that.
‫Now we'll go to our controllers.
‫Let's create a file called Home Controller.
‫And Pascal case is going to be the convention you want to use here.
‫This is a class.
‫And like I said before our controllers will have their own namespace.
‫So we're going to say namespace app slash.
‫Controllers.
‫Okay.
‫And then we'll create our class called Home Controller.
‫And and again this is the the app namespace gets auto loaded.
‫So we're not going to have to require home controller or listings controller or anything like that.
‫That's going to get taken care of for us because of the PSR for auto loader that we're using.
‫So for the home controller at the moment I'm just going to create a constructor.
‫So public function oops what am I doing here.
‫Function double underscore construct.
‫And for now let's just do di and we'll just say home controller.
‫All right.
‫So I'm going to save that and reload.
‫And we see home controller because we're on the home page right.
‫Just slash.
‫And that route is pointing to the home controller.
‫And it's loading the index.
‫Now we're seeing this because it's in the constructor.
‫Anything you put in the constructor will run regardless of what this method is.
‫In fact, let's create the index and run it and just make sure that that works.
‫So public function index.
‫And let's I'm going to cut that.
‫I'll keep the constructor because we will need it.
‫And then let's go in here and say home controller at index.
‫So now if I reload the page we should see that.
‫So it's loading this method.
‫All right.
‫And if we were to add another row, like, you know something whatever Uri.
‫And then home controller at create.
‫And then if we create a create method in here then that would run if we visit that route.
‫All right.
‫So what I want to do in the next video is now transfer everything from our just our controller files
‫here into methods.
‫Okay.
‫So we'll start to do that next so that our application goes back to a working state.