📄️ Section Intro
This section is going to get a little hairy. We're basically going to change the entire infrastructure of the project. We're going to be structuring it more like a real-life application. Similar to how Laravel is structured but of course much much simpler.
📄️ Folder Refactor
Alright, so before we move on to create data and get into authentication, etc, I want to improve the infrastructure of the project a bit. We will be doing a couple big refactors throughout the project. Here, we will move some things around and then we're also going to be using something called namespaces in a little bit.
📄️ Custom Autoloader
So we have our Framework folder and this will be all classes. Right now we are manually requiring the files in our public/index.php file. This is fine for now, but as we add more classes, it will get messy.
📄️ composer-psr-4-autoloader
Composer & PSR-4 Autoloader
📄️ Namespaces
If you have ever used Laravel or Symphony, you have probably seen namespaces. They are a way to organize your code and prevent naming collisions. They are also a way to autoload classes without having to use require statements.
📄️ Router Refactor For Controller Classes
At the moment, we are using single files as our controllers. That's fine for a small project like this, but it can get messy. We're going to be using controller classes instead.
📄️ Controller Classes
We have our router working with controller class methods. Let's add some more routes to routes.php:
📄️ Error Controller
Right now, we have a controllers/error folder with a 404.php file. I don't want to do things that way so you. can completely delete the error folder.
📄️ Handling Route Params
For our show page, we are getting the ID from the query string in the URL. This is not the best way to do this. Instead, we should be using route parameters. This will allow us to have a URL like /listings/1 instead of /listings?id=1. So yes, we are going to refactor the router again.
📄️ Section Wrap Up
Alright, so I just wanted to say a couple things before we wrap this section up.