‫All right guys.
‫So now we're going to get into creating our router.
‫Basically we want to map specific URLs like let's say we have slash slash listings right.
‫So if we load that URL then we want it to map to a specific file called a controller.
‫So the controller will handle fetching any data from the database through PDO.
‫It'll handle loading the view, etcetera.
‫Now in order to do this, we need our routes to be able to to to route through our index PHP file.
‫So we can say if route equals this then load this file.
‫And if you're using the built in PHP dev server, there's not really any extra steps to take for that.
‫However, if you're using Apache, which you will be in production, either Apache or Nginx, but if
‫you're using Larrigan or Xampp on your local machine, then you're using Apache and there's an extra
‫step that you're going to have to take.
‫And that is creating a dot access file.
‫Okay.
‫So what you want to do is in your public folder and I'll do it even though I don't really need it in
‫my development environment.
‫So I'll create a new file dot h to access.
‫And then I have right here in the in the course docs what you want to put in there.
‫So we're adding this rewrite condition and basically mapping everything that's not an actual PHP file
‫through index dot PHP.
‫All right.
‫So just go ahead and add that.
‫And you want to put it in the public folder because that's where your index PHP file is.
‫And that's what it's looking for here.
‫All right.
‫So now that we've done that we want to now create our routes array.
‫So I'll close that up if you guys want to you know keep the documents open.
‫You can.
‫But we're going to start off just having the the routing logic in the index PHP file.
‫And then we'll move it.
‫Like I said we're going to refactor a lot in this in this project.
‫So I'm going to get rid of the load view.
‫And let's create an array called routes.
‫Oops.
‫Routes.
‫And basically and like I said before, this is going to change about 3 or 4 times.
‫But right now all I want to do is have an associative array that has the URL and then the controller
‫that I want it to map to.
‫And the controller is going to be a file right now.
‫Later on we'll have our controller classes with certain methods we can map to.
‫Right now it'll just be a file.
‫So let's say controllers.
‫We'll have a folder called controllers and then home dot php okay.
‫So that's what I want.
‫The home route or the index route to map to.
‫Then we'll have the listings.
‫So let's say listings slash slash listings.
‫And we want that to map to controllers.
‫And we'll have a folder called listings because we'll have multiple controllers in here.
‫And this is going to be index dot PHP.
‫And typically what this is will do is list is get all the listings okay.
‫Later on we'll be able to have a route that's like listing slash and then ID, but we're not going to
‫do that just yet.
‫We're going to make it really, really simple and then kind of work up incrementally.
‫And then let's do the create page as well.
‫So it'll be slash listings slash.
‫Create.
‫And that's going to be our form to add a listing.
‫And that's going to map to controllers, slash listings, slash and then create dot PHP.
‫And then the last one I want to put here is 404 error.
‫So we'll say 404 and set that to controllers slash error slash 404 dot PHP.
‫Okay.
‫So we will have more routes.
‫But that's what we're going to just stick with for now.
‫So that's that's the array.
‫Now we need to add the router logic so we can get the current Uri using the server Superglobal.
‫So we're going to say server and we can get request underscore Uri.
‫Okay.
‫In fact let's do an inspect.
‫We created inspect actually we'll do Inspect and die which we created in the last video.
‫And we want to look at the Uri.
‫So I'm going to reload the page here.
‫And you can see it's just slash.
‫Now if you go to slash listings.
‫We get slash listings.
‫And if you aren't seeing this, if you're seeing like page doesn't exist or something, then you probably
‫have your your access file set up wrong.
‫So just make sure that you have this exact access.
‫All right, so now we know we can get the Uri.
‫So now we just want to basically check to see if that Uri is is in our routes array.
‫And if it is then we'll require that controller or that file.
‫So let's get rid of this.
‫And let's say if and we're going to use the function array underscore key underscore exists because
‫the keys are the actual URIs.
‫And that array key exists.
‫Function takes in.
‫It's going to take in the Uri which is coming from the server Superglobal.
‫And then it's going to take in the array.
‫So roots.
‫So if it's in there then let's say require.
‫And we're going to require the.
‫Um, base path.
‫So we're going to use our base path function.
‫And then in the routes array we want the Uri.
‫Which is going to be coming from here.
‫So let's say we go to slash listings.
‫Then it's going to load routes listings, which is this right here which will load this value.
‫So it's going to require this file right here.
‫If we go to slash listings okay.
‫If we go to slash listings create then it's going to load this one.
‫Now.
‫If it's not there, right?
‫If it's not in our array, then let's load the 404.
‫So we'll say require and base path.
‫And we want roots.
‫And 404.
‫Okay.
‫And we'll we'll change up the error handling and all that later on.
‫I'm just this is the simplest way to do this right now basically.
‫All right.
‫So let's go back to the home page.
‫And right now we're going to get an error.
‫It's saying that controllers home app doesn't exist.
‫So we want to create that.
‫Let me just close these up.
‫Make sure you create it in the root.
‫And we want controllers.
‫Inside controllers.
‫We're going to create home dot PHP.
‫And what we want to do is simply load the home view just like we were before.
‫Except now it's in this, this controller, as opposed to just writing the index.php so we can say load
‫view.
‫And we want to load the home view.
‫All right, let's save that.
‫Reload.
‫And now it works.
‫So let's add the other things as well.
‫So.
‫This link down here.
‫This show all jobs right now goes to listings dot HTML.
‫Let's change that to go to just slash listings.
‫So in the views and then in the home view.
‫Down at the bottom here.
‫Let's see.
‫Right here.
‫Show all jobs.
‫We're going to change the link right here to just go to slash listings.
‫Okay, so if I go back and reload and I go to slash listings and we get an error that just says that
‫that file doesn't exist.
‫So let's create it in controllers.
‫We're going to create another folder called listings.
‫And any any listings controllers will go in this folder.
‫So let's create a file here index dot php.
‫And for now, let's just echo.
‫We'll just say we'll just say listings for now just to make sure that this actually works.
‫Okay.
‫So we get listings.
‫Ultimately what we want to do is load the view the listings view.
‫But we'll just do this for now.
‫And then what else do we have in our routes.
‫We have this listings slash create.
‫So that's going to go to controllers listings create.
‫So in listings I'll say create.
‫Whoops.
‫That should be a file.
‫So create dot php.
‫And we'll just echo out create listing.
‫Okay, so now if I go back here, let's make this button go to that.
‫Because right now this button is just going to whoops.
‫It's going to that HTML file post job HTML.
‫So let's change in the partial in the let's see nav bar where it says post to job.
‫Let's change that.
‫To go to.
‫Slash listings.
‫Slash.
‫Create.
‫And then there's also a button in the bottom banner to post a job, so we'll change that as well.
‫So bottom banner.
‫Get rid of that.
‫Slash listings.
‫Slash.
‫Create.
‫And you can call it something different if you want to add a different route.
‫Like, I don't know, just slash post job or something like that, you can.
‫But again, I would recommend sticking to what we do in the course and then changing things afterwards.
‫All right, so if I reload and I click on this button here now it takes me to that create listing.
‫And of course we can load our view do whatever we want in that in that file that controller.
‫All right.
‫Now let's do the error.
‫So if I were to go to let's say slash hello.
‫You can see now it's looking for my 404 error controller.
‫So let's create in controllers a folder called error.
‫And in there I'm going to create 404 dot PHP.
‫So I'll just say PHP and let's echo.
‫Error or wheel echo 404.
‫Now if I reload, if I go to anything that is not in that routes array, then it's going to take me
‫to that page or it's going to load that controller.
‫So I think this is a good place to stop.
‫And in the next video, instead of just echoing things out of these controllers, we'll create the view
‫and we'll load that.