‫All right.
‫So now we're going to.
‫New error controller.
‫And one thing I just want to mention is in the in the video.
‫I forgot to add my comments to the controller methods here, so I just added this show.
‫All listings return void show the create listing form.
‫Show a single listing and in the home controller just show the latest listings.
‫So I just like to keep it clean with comments.
‫Now at the moment we have this error controller, this this folder with a 404 dot PHP that just loads
‫a view.
‫Now, this isn't the way that I want to do things.
‫I want to stay consistent with our classes.
‫I also don't want to have to create a separate folder for everything here.
‫So let's just completely delete that error folder and let's create a new file called error.
‫Controller dot PHP.
‫And then I'll just copy what we have in the home controller.
‫Paste that in.
‫And we're not going to be using the database here so we can get rid of that use.
‫We're going to change this to error controller.
‫We're not using the database.
‫We don't even need a constructor.
‫So get rid of that stuff.
‫All right.
‫Now, what we'll do is create a specific method for different types of errors.
‫So for instance not found.
‫So in this comment let's say 404 not found error.
‫And we're going to change the name of this to not.
‫Found, and then it's going to take in a message, but we'll set a default message of resource not found.
‫That way.
‫If we don't add a message, it just says resource not found.
‫But if we wanted to, let's say we're working with the job listings, you might want to pass in a message
‫that says listings not found or listing not found.
‫All right.
‫We can just get rid of all this.
‫Actually, we can keep the load view because that's what we're going to do.
‫But before we do that, let's set our Http response code to 404.
‫And then let's load the view.
‫Error.
‫We're going to change how the views work as well with the error, and then we're going to pass in here
‫a couple of things.
‫The status.
‫And the status is going to be the status code of 404, and then also the message, so that we can load
‫that into the view.
‫So that will be the message and that's it.
‫So that's an example of one type of error.
‫You might have another where you know you want to do a 403 unauthorized.
‫So we might as well just add that while we're here.
‫So I'll just copy this function or this method, paste that in and we'll say 403.
‫Unauthorized error.
‫And let's go down here.
‫Let's change this to.
‫Unauthorized.
‫And for the message.
‫Let's see.
‫What should we do for the message?
‫We're going to say you are.
‫Not authorized to view this resource.
‫So that'll be the default message.
‫And then we want the code to be a 403.
‫And we want to still load the error view.
‫But now we're just going to change the status.
‫And again we're going to pass the message in.
‫Okay.
‫So that takes care of the controller.
‫Now if we look at the view folder which is in the app folder, we have an error folder with with these
‫403 404.
‫Again we don't want to create a new file for every single error.
‫So instead of doing that let's see I do want to copy.
‫Think I want this HTML, right?
‫Yeah.
‫So let's let's create an views a file called error dot view dot PHP.
‫And then what I'm going to do is copy from 404.
‫I'm going to grab that.
‫And then paste that in the error view.
‫And then I'm just going to delete that error folder.
‫We no longer need that.
‫So views error.
‫That's going to go away.
‫So error dot view.
‫Let's just now change since we're going to load this view.
‫And we're passing in the status and the message.
‫Let's change this right here.
‫To output the status.
‫And let's change this to output the message.
‫All right.
‫And let's see, we have head nav bar.
‫Yeah.
‫We'll leave off the top and bottom banner so that that should be good now.
‫The error controller.
‫One thing I forgot here is that I want to make these static methods so that we don't have to instantiate
‫a new error object, because we're not going to need to create more than one instance.
‫So we're just going to go right here and say public static function not found and public static function
‫unauthorized.
‫And the file, the error controller file is going to be auto loaded, right.
‫Because it's in the app namespace.
‫And we set up our auto loader to automatically load classes from the app and framework folders.
‫Now where we want to use this is in the router.
‫So let's go to router PHP.
‫And the way we're handling this now is we're calling this error.
‫And then in that error method that's where we're loading the code.
‫And we're loading the view.
‫So instead of doing this we can completely get rid of this error method.
‫And instead of this we can just say error controller.
‫And then since it's a static method, we use the the scope resolution operator, which is the double
‫colon.
‫And then whatever method we want, which in this case is going to be not found.
‫Now remember the error controller is in the app namespace.
‫So if I were to save that and go to let's say slash test which doesn't exist, it says error controller
‫not found.
‫We need to use that error controller from the namespace.
‫So up here let's say use app slash controllers slash error controller.
‫And then if I reload we get 404 resource not found.
‫And that should do it.
‫Now we have an error controller.
‫And you can see again how much our code has has just been cleaned up.
‫The controllers folder now just has our three controller files.
‫The views.
‫We no longer need an error folder, we just have this one error view that handles all different types
‫of errors, just gets passed in a message and a status.
‫All right.
‫So now what I want to work on next is routing parameters.
‫So I want to be able to pass in.
‫For instance if we go to the details page I don't want to have to do ID equals one.
‫I just want to say listing slash and then the id.
‫So obviously we need to change our router to handle that.
‫And I know that we've changed the router quite a bit after this.
‫The only other change I want to do is the have the ability to pass in middleware for our authentication
‫later on.
‫All right.
‫But yeah, in the next video we'll work on handling the route params.