‫Okay, so our router is now capable of taking in routes like this.
‫And we have our home controller created.
‫And we have our index method which right now is just dying and echoing out this here.
‫So in this video I want to take everything from our controller files and put them into classes and make
‫it function just like it was.
‫And then we can actually delete like the whole listings folder because we won't need that anymore.
‫We're also going to create a separate just an error controller rather than doing a separate file for
‫each status.
‫So let's handle the home page first.
‫Right.
‫We already have kind of the skeleton of the the class here.
‫So we are using a database in the home controller because we're getting those listings right.
‫In fact let's open home dot PHP.
‫So we basically want to bring this into our home controller and and have this functionality in the index
‫method.
‫Now, now we are like I said we're using the database.
‫So we're going to have to come in here and we're going to say use framework.
‫And we want to do slash database.
‫Okay.
‫So that's the first step.
‫Now this here I'm going to put this in the constructor because we're instantiating the database.
‫So when we use the home controller when it's run it'll automatically connect to the database and give
‫us that variable.
‫Now this db I want that to be a property of the class.
‫So what we'll do is come up here above the constructor and let's say protected.
‫DB.
‫Now, since it's a property of the class, we need to just add this in front of it.
‫So we'll say this arrow db equals new database with our config.
‫Now we have access to this db and any method we create.
‫So let's go back to home dot php.
‫Let's grab these.
‫And let's get rid of that.
‫Paste those in.
‫And then really, the only thing we need to change here is the DB needs to be this DB.
‫And then we're going to run the query fetch all.
‫And then we're going to load the view and pass in listings.
‫So let's save that and let's reload.
‫And there we go.
‫So you see how much cleaner this is than this?
‫But I didn't want to start with this because this would confuse a lot of people.
‫So I like to, you know, make incremental, incremental changes so that you can learn what's actually
‫happening.
‫So that takes care of the home page.
‫Now we want the listing controller.
‫And that's where not only will we fetch listings for the listings page, but we'll also do the create.
‫We'll do the the delete, the update, all that stuff.
‫We can now get rid of the home dot PHP.
‫We don't need that anymore.
‫So just delete that file.
‫Everything should still work.
‫Now in controllers.
‫Let's create a new file called listing.
‫Controller dot PHP.
‫And kind of the same deal.
‫We're going to have the namespace, we're going to use the database and so on.
‫So I'm just going to copy the home controller and then paste that in the listings controller or listing
‫controller.
‫And of course we want to change the name of it to Listing Controller.
‫And again we don't have to require this anywhere because it's being auto loaded and it's being instantiated
‫in the router right here.
‫So it's getting the proper controller which will be the listing controller.
‫If we go to slash listings.
‫Right now we don't have a route, but we'll add one and then it will down here instantiate whatever
‫method is in the route or coming from the route.
‫So before we even do anything else with the listing controller, let's just add the route for the listing
‫index.
‫So I'll copy that down.
‫This is going to be for slash listings and and it's going to be Listing Controller index.
‫So we'll save that.
‫Okay.
‫If I reload, we're just going to get an error because it says listing controller not found.
‫So let's just finish this up.
‫Now this part the constructor and the database is all going to stay the same.
‫So we're just instantiating database putting it into this DB.
‫And we can use it anywhere we want.
‫And then down here in the index this is going to be very similar.
‫We just don't want the limit because this is for the listings page.
‫And we want to show all listings.
‫So now let's save that and reload.
‫And there we go.
‫It's working.
‫And now when we want to create, like let's say when we click this right here and we go to slash create,
‫we want to load the create method.
‫So let's do this for now.
‫We'll go to our routes and let's copy this down.
‫And this is going to be listing.
‫Slash create.
‫And it's going to go to the listing controller.
‫So same same controller, same class, but it's going to go to the create method.
‫So we'll save that.
‫Now let's go to our.
‫Uh, class here and let's say public function.
‫Create.
‫And if we go in our old create dot PHP, all it's doing is loading a view.
‫So we'll just grab that for now.
‫Paste that in, save it, reload the listings, create URL.
‫And now that works.
‫All right.
‫So now let's do the show right or the details page.
‫So if we click on this right now it's not working because there's no root.
‫And by the way we can now delete listings create we can get rid of that.
‫We can also get rid of the index listings index.
‫So let's delete that.
‫And that the home page should still work.
‫Your create should work and your listings.
‫If we click on this show all jobs that should work.
‫Now we want to do this page which doesn't work.
‫So it's going to let's see.
‫We want to create a route first.
‫So we'll copy this down and we're going to load.
‫If it goes to just listing.
‫And we're going to make this so that we can pass in an ID through the route.
‫But for now it's just going to be listing.
‫And then that's going to call listing controller at show.
‫And we can get rid of all these old routes.
‫So now that's going to call show, but it doesn't exist yet.
‫So we get an error.
‫So we'll go into our listing controller and let's say public.
‫Public function.
‫Show.
‫And let's see, we'll go into our show dot PHP and let's see what we're doing here.
‫We're getting the ID, the params, we're setting params array for a prepared statement.
‫We're making the the query.
‫And then we're loading the view so we can just grab all that.
‫And in the show paste that in and we just need to add this to the DB.
‫Sort of be this DB query.
‫So let's reload.
‫And there we go.
‫All right.
‫And we can now delete the entire controller's listings folder.
‫Don't need that.
‫So we're really cleaning things up here.
‫Now in the next video I want to create an error controller.
‫I don't want to have this folder with a bunch of different status code for files.
‫I don't want that.
‫I want to have a control, a class controller or a controller class, I should say with a not found
‫method.
‫For instance, for 404 seconds, an unauthorized method for 403 seconds, things like that.
‫So we'll get to that in the next video.