Review Current Structure 0:00So we're about ready to move on to routing, but before we do that, give me 30 seconds to go over our current structure just to make sure we're all on the same page. Okay, so we have our entry point. The entry point loads a bootstrap file. This kind of loads everything that we need for the time being. So we fetch some configuration, we form a database connection, and then we also instantiate a query builder passing in our instance of PDO. So the query builder returns an instance of this class that contains any number of helpful methods for querying the database. In this case, select all. Okay, so now we have our database, we call select all, we pass todos, Routing with PHP Files 0:35In this case, select all. Okay, so now we have our database, we call select all, we pass todos, so that essentially says to MySQL, give me all records from the todos table. Next, we load a Vue, is how we're calling it, and we work with that data that we fetched from the database. Okay, so now, how exactly does routing work in PHP? Well, it works in a couple ways. One, in my main directory, or often you'll want to set the document route to something like a public folder for security reasons. But for now, yeah, you could create an about.php file,to something like a public folder for security reasons. But for now, yeah, you could create an about.php file, and then here you could say about us. So now, if we go back to Chrome, I could visit about.php, and we've loaded it. And now the exact same thing is going to be true for contact. So especially for small, more static sites that only require maybe a PHP form, because you're going to send an email or something like that. This is actually a fine way to go, just as long as you protect yourself. And like I said, do some research on setting the document route to someplace outside of the core of your project.And like I said, do some research on setting the document route to someplace outside of the core of your project. But it's a little beyond our current skill set for now. But anyways, yeah, that would work. So your homepage, your about page, and then finally, your contact page. Now, in your homepage, right here, yeah, you could have some links here. So you could say nav, and then here, take me to about.php. You get the basic idea. This one will be contact, like so. And if we come back and refresh, whoops, we're missing closing anchor tags.This one will be contact, like so. And if we come back and refresh, whoops, we're missing closing anchor tags. Okay, but yeah, now we can switch to the about page and to the contact page. So yeah, like I said, this is fine for very basic websites. So if you're building kind of a landing spot for your restaurant or something, and there's three or four pages, there's a map and a contact form, and really, you'll never come back to it. That's really all it needs to exist. In those situations, I really think this is perfectly fine. However, for anything a little bit larger, I think you're going to see that, Why Build a Router 2:44In those situations, I really think this is perfectly fine. However, for anything a little bit larger, I think you're going to see that, well, linking the URI to a specific page begins to break down. What if you want a file name to be different than the URI that you see here? Or you want this to direct somewhere else that's not contact.php or about? Yeah, so when you need more flexibility, you might want to set up some kind of router. Now, once again, I want to warn you, I'm going to show you a very basic example of how you might go about doing this. But I think for most of us in real life, you're not making your own router. You're pulling in an existing router,But I think for most of us in real life, you're not making your own router. You're pulling in an existing router, or you're using a web framework that provides one for you out of the box. So I'm going to show you how to do this so you can wrap your mind around it. But once again, remember that really everything we've done so far, you can find good tooling that will give this to you for free and in a much more mature and battle-tested way. Battle-tested fashion. Nonetheless, it's still good for you to understand some of the basic workflow here. I think it's really going to help you. Reorganizing Project Folders 3:46Nonetheless, it's still good for you to understand some of the basic workflow here. I think it's really going to help you. Okay, so to begin, let's do a little cleanup here. Here's our project, but we have this bootstrap file and then a database. And I know I'm going to add a router and maybe a request class. So why don't we put this all within a core directory? So anything that's kind of core to our little makeshift framework is going to go in there. So our database can go in there. Our bootstrap can go in there. Next, we have these views here, right?Our bootstrap can go in there. Next, we have these views here, right? So about, contact, and index. Maybe those can go within a views directory. Like so. And then finally, I'm going to set up a controllers directory, and we'll talk about that quite a bit more. Okay, and by the way, one last thing. Let's make sure that we follow the same convention here. So contact.view and about.view.Let's make sure that we follow the same convention here. So contact.view and about.view. It doesn't matter. We're just following a convention there. Okay, so now this is looking a little bit cleaner. So index.php is the entry point into our application. But right now, it's loading to-dos. So it's still sort of serving the purpose of the entry point into our app, but also the home page. So we want to separate those.but also the home page. So we want to separate those. Instead, I'm going to create a controllers, and this will be my index or home controller. And now I can put that within here to start. So now notice our controller, quote unquote, will interact with the database, get anything it needs, and then it defers to a view. Okay, so now, yeah, index.php, the entry point, this can just be responsible for bootstrapping and setting up our project. Mapping URIs to Controllers 5:21Okay, so now, yeah, index.php, the entry point, this can just be responsible for bootstrapping and setting up our project. Now, here's how I want it to work. Ideally, I want to set up a handful of routes. So I want to do something kind of like this, where I say, well, the home page, so if there's no URI, I want that to go to controllers index.php. But if we go to about, so if that's the URI, so if the user does example.com slash about, yeah, then I want to go to controllers slash about.php.so if the user does example.com slash about, yeah, then I want to go to controllers slash about.php. Or if it's about slash culture, so that would be this. Well, in that case, we're going to go to controllers about culture.php. And then finally, if the URI is slash contact, then we're going to go to controllers contact.php. All right, so does that make sense? Let's go ahead and create some of these. Controllers about culture.php. And then finally, controllers contact.php.Controllers about culture.php. And then finally, controllers contact.php. Okay, so for now, about, well, we don't have any data to compile, at least not yet. So I'm just going to require views about.view.php. And we'll do the same thing for contact. And then about culture, yeah, we can say about culture and create that. Our culture. Okay, so does this all make sense? So we've decided that we want to set up a handful of routes that specifyOkay, so does this all make sense? So we've decided that we want to set up a handful of routes that specify what the association is between a URI and a controller. A controller, in many ways, is an entry point for a route. So when the user visits example.com slash about, well, the router is going to read this and figure out, oh, well, I need to direct traffic to this controller. The controller is then going to say, okay, in order to do my job, here's what I need. I need this data.okay, in order to do my job, here's what I need. I need this data. So I'm going to ask this guy to make a database query to give me anything I need here. And then I'm going to be responsible for delegating to this view or to this template to render everything onto the page. So the controller kind of interacts with people. It's almost like the project manager, so to speak. It gets the data it needs, it passes it off to the view, and then the view renders it.It gets the data it needs, it passes it off to the view, and then the view renders it. And this is kind of the basic introduction to Model-View-Controller, MVC, if you are familiar with that. Otherwise, don't worry about it yet. Okay, so this looks good, but it might be nice to have a dedicated routes file. So now whenever I want to edit a route, and as you can imagine, when you build a project, you'll spend a lot of time defining routesand as you can imagine, when you build a project, you'll spend a lot of time defining routes and creating connections between URIs and endpoints. So it might be nice to have one file that's very simple that I can go to. Next, well, maybe it's going to be an object, and I can just say, define these routes for me. That might be nice. Or what you might see with some frameworks is something like this, where you call one reference per definition. So in this case, I would do it multiple times.where you call one reference per definition. So in this case, I would do it multiple times. When we go to about, then I want to go here. Or if you use something like Laravel, it might take the shape of something like this, but it's still mostly the same thing, okay? The Laravel version is just much more mature naturally, because what we're going to do is very simplistic. Okay, so it sounds like for this file to do its job, it needs some kind of router object. Implementing Router Class 8:58Okay, so it sounds like for this file to do its job, it needs some kind of router object. So I need a router class, and then we will need to create a define method. Okay, well, it sounds like I need to create a router.php file. And then very quickly within our bootstrap, we're going to pull that in, core router.php. And then while we're here, we need to update these to core database. And that's because we did move these into the core folder. So you're always starting at the project route,And that's because we did move these into the core folder. So you're always starting at the project route, going to core, then into database, and then give me the connection file. All right, so we said it needs a define method, right? That's the API we want. We can name this anything we want too. If you want it to be register, whatever feels right to you is what you should do. And then you make the class offer that exact functionality.whatever feels right to you is what you should do. And then you make the class offer that exact functionality. All right, so it's going to define some routes, and I'm going to store them on the object. Something like this, this routes equals routes. Next though, how exactly would this work? Well, we're going to set up a new router, I guess. And then we would say require routes.php. So we've created our variable called router. We've required routes.php.So we've created our variable called router. We've required routes.php. So that file has access to a router object. It can then call a define method right here. It will pass it an array of routes. And the only thing we do with that is we assign that array to this property. So now protected routes will be equal to this array. Okay, but we're not done yet. So we have loaded the routes. But finally, maybe we need to load the controller associated with the URI.So we have loaded the routes. But finally, maybe we need to load the controller associated with the URI. So maybe we could say router.direct. So direct the traffic, I don't know, and then require that. Let's see what that might look like. We add a method called direct. And well, what would we do here? What we basically want to say is, well, we have a URI, right? So let's say it's about slash culture. Well, what we want to say is, given the current URI that the user has requested,So let's say it's about slash culture. Well, what we want to say is, given the current URI that the user has requested, let's say it's this. Well, if we have a route that matches that, then we want to load the controller associated with it, right? So we're going to go in here and we're going to say, is there anything with about slash culture? Yes, there is. And it looks like the controller associated with it is this one. So that is the file that we want to require or load.And it looks like the controller associated with it is this one. So that is the file that we want to require or load. So we could do it like this. We can use a new function, array key exists. Now, as you can see here, it accepts a key and the search. So the key will be ideally about slash culture. And we're going to set that up in a bit to make sure that that's how it works. And then we are going to search through our routes. So array key exists is going to look through our array of routes. And it's going to look for a key that matches this.So array key exists is going to look through our array of routes. And it's going to look for a key that matches this. And in this case, about culture. So is there any key here that is about slash culture? And yes, right here. So remember, this is your key, this is your value. So we do have a key that matches up. So I want to load this file. We can do it like this. If array key exists, if we found one,We can do it like this. If array key exists, if we found one, then we're going to return just a file path, this routes URI. So if it helps, it's going to be this. Go into the routes file and return to me the value associated with about slash culture. So we are returning this. The index method calls that and requires the response. So basically, it's just a more complex way of saying require controller slash about culture. But now it can be dynamic. So if we come back, if we did not find anything, we might want to throw an exception.But now it can be dynamic. So if we come back, if we did not find anything, we might want to throw an exception. So an exception is our way of saying something exceptional occurred here, and I don't exactly know what to do. So no routes defined for this URI. Okay, so this is starting to look good. So we come back to our index file. Let's make sure we update our path. We're now loading core slash bootstrap. We knew up our router, we load any routes,We're now loading core slash bootstrap. We knew up our router, we load any routes, those routes will register URIs and controller endpoints. And then we direct the traffic. And if we come back, that's going to return to us a route ideally that we require. However, notice that to direct the traffic, we do need the URI. And if we come back to the browser, it's squawking about that very thing. Missing argument one. So yeah, we want to basically send through the URI that is currently listed here. And now you can see it is loading the proper controller.So yeah, we want to basically send through the URI that is currently listed here. And now you can see it is loading the proper controller. But of course, we're hard coding it here and then also here. So let's bring this back. And you'll see that if we change this to about, it loads the about page, even though the URI doesn't match. Or if we go to contact, it's going to load the contact controller. And then the, by the way, the contact controller loads the contact view, like so. All right. So now how do we fetch this, this URI?All right. So now how do we fetch this, this URI? Let me show you something. We're going to die, or let's just do this. Let's var dump. And I'm going to show you this new super global. PHP has these super global arrays like $get, get any get items from the request. We have none. Or any post. Or there's one called server.Or any post. Or there's one called server. And this is going to give you a specific server information. Now, in our case, what we are interested in here is the request URI. So the URI is like this section right here, about slash culture. Oh, and by the way, if you like the look of this and it's all jumbled text, if you're working along, you can pull in a Chrome extension called var masterpiece. Just Google it. You'll have it in five seconds. And it'll basically format the output in a nice fashion.You'll have it in five seconds. And it'll basically format the output in a nice fashion. Okay. So if we were to say server request URI and come back, there we go. Now we get that. So we're getting close, but I don't want this forward slash. And then if there was a slash at the end, what I really want is about slash culture. So we're going to use another function here called trim. Now, generally trim is going to trim off white space at the beginning or the end. However, you can also specify a second argumentNow, generally trim is going to trim off white space at the beginning or the end. However, you can also specify a second argument that designates which characters should be trimmed. So now I'm saying get this item and I want to trim it and get rid of any dangling forward slashes at the beginning or the end. So now you'll see, well, let me show you one more time. Before forward slash at the beginning and the end, but we're trimming those off. So now they're gone. So this is exactly what I want.So now they're gone. So this is exactly what I want. So we could say, save that to the URI. And then redirect and pass in the URI. Okay. So our culture, the homepage, the about page, the contact page. And then once again, the about culture page, like so. And that's it. We have a very basic router in place. It's not overly flexible.We have a very basic router in place. It's not overly flexible. We don't have wild cards, but for a small little project where you need to add just a little more flexibility to your routing, this will get you started and then you can grow it from there. All right. But still, maybe we can clean this up just a touch more. For example, maybe I could do something like this where I say router. Well, I'm going to load routes.php. And then I want you to direct traffic to this URI.Well, I'm going to load routes.php. And then I want you to direct traffic to this URI. And we're going to require the file that you returned to me. So right now it's going to fail, right? There is no load method. And real quick, in fact, what I want you to note is that I'm writing code and I'm referencing methods that don't exist. And that's a good habit to get into. Try to define it in the best possible way for your mind. What would make you understand this best?Try to define it in the best possible way for your mind. What would make you understand this best? So you write the code out first and then you see it fail and then you make it work. So it sounds like I need a static method called load where we accept the file. We will load the file, but it's not going to work, right? Undefined variable router within routes.php. So we've basically at this point required routes.php. But don't forget, routes.php expects this router variable. So I need to make sure that's available to it. So here's what we're going to do.So I need to make sure that's available to it. So here's what we're going to do. A static function is not a instance method. So it's just kind of like a global method that can be called at any time. If I actually want a new instance of router, I can use this keyword here, static or self. I often do static. So think of this as just a way to say, create a new router instance. That's it. So we have our instance.That's it. So we have our instance. We're now loading our routes.php. Routes.php has access to this router instance. So now when it calls define, once again, it's calling this method right here that updates the routes property. But now what else? So we've loaded it, but now we have something different. I called a method, but then I called another method directly off of it. We call this chaining.I called a method, but then I called another method directly off of it. We call this chaining. So if you want this to work, well, it would be equivalent to this. Router equals router load and then router direct. Okay. So that means the result of the load method needs to be a router instance. So basically, and you'll do this a lot, we're basically saying I'm going to call this method and I want you to return to me an instance of this same router. So that I can continue chaining.and I want you to return to me an instance of this same router. So that I can continue chaining. We can do that by referencing this. However, notice that I'm not going to be able to do this here like I did it here or here. How come? Well, first, let's see it fail. Undefined variable this. And that's because once again, this is a static method. So this is not an instance method that has access to this object.And that's because once again, this is a static method. So this is not an instance method that has access to this object. We created an instance method, but we saved it to router. So that is our instance. And I'm going to return that. Okay. So now we load our routes that will require the routes.php and let them define any routes. And then we say router direct. That will determine the controller that we then require.And then we say router direct. That will determine the controller that we then require. Okay. Come back and we're back to where we were. Home. About. Contact. Whoops. Contact. And now notice what's nice about this isContact. And now notice what's nice about this is if you later decide that you want the URI to be contact our company. Well, now, yeah, you are not linked to a file name. So all you have to do is change this and it'll work. Oh, and by the way, if we did reference a URI that doesn't exist, here's that exception that we threw. So we'll talk about exceptions in the future and how to handle them. But we did respond correctly here. The only thing we would want to do at this point is catch that exceptionBut we did respond correctly here. The only thing we would want to do at this point is catch that exception and say, we don't know what route this is. So maybe we'll display a 404 page is how we might refer to it. Anyways, this is looking pretty good. So if I go back to index.php, this is fine. We can bring back what I had before. And I think this just cleans it up a touch and we'll still get the same thing. You might want to keep the URI as it is. Or what you might want to do is eventually build up to like a request class.You might want to keep the URI as it is. Or what you might want to do is eventually build up to like a request class. And this class will be responsible for fetching information about the current browser request. So maybe I could say something like, just give me the URI of the request. And now that could be responsible for this bit of code here. And I will return that. This is very, very rudimentary. But I want to get some of these concepts in your head. And then that way, you'll understand how it all works. But also, once you bump up to a dedicated tool or a framework or a library,And then that way, you'll understand how it all works. But also, once you bump up to a dedicated tool or a framework or a library, these concepts are going to make perfect sense to you because you've already written them to some extent on your own. And they won't be new to you. Once I teach you about Laravel, you're going to see it has its own routes file. And it's going to feel that much more familiar to you. OK, but anyways, so we have a request class now that has a single static method. So now, in index.php, we could always say, request URI. And let's make sure that we load that.So now, in index.php, we could always say, request URI. And let's make sure that we load that. And on that note, you'll see that we're manually loading basically every file we need. Later, I'm going to show you what we call autoloading. And that will basically automatically pull in any class that you require. But for now, we're still just going to do it manually. All right, come back, refresh. That looks good. Go to the homepage. That all looks good.Go to the homepage. That all looks good. So now, let's do this. Our bootstrap file, well, it kind of doesn't make sense anymore for it to return a database instance. So one thing you could do, really, there's so many different ways to set this up. But we could always create an app variable like so. And then maybe store these keys in here. So if you ever want to fetch your config, you can just say app config. And that's going to give you the full array of your site's configuration. Next, we'll do the exact same thing here.And that's going to give you the full array of your site's configuration. Next, we'll do the exact same thing here. App database. So now, if I were to come back and I die and var dump our app, that will then defer to this variable right here. And by the way, we need to do app config. Anyways, if we were to come back and refresh the page, yeah, we have our array that contains the configuration and our database. So now, we would only need to update our controller. We could just say app database.So now, we would only need to update our controller. We could just say app database. Once again, kind of rudimentary. There are some cleaner, better ways to do this. But just to get you started, this is something that you might consider. So if I come back, yeah, that's still going to work. About our culture, everything works like it did before. But yeah, you've set up your first rudimentary router. Router, load this file, and then direct traffic. And we will require wherever you direct it to.Router, load this file, and then direct traffic. And we will require wherever you direct it to. So if the router is about slash culture, that's going to direct us to about culture controller. The controller, maybe we could say name is Laracast. So we fetch some data or we've compiled some data. We now delegate to our view or our template. And then we could say our culture at name. And if I come back, we can work with that data. All right.And if I come back, we can work with that data. All right. So yeah, you're seeing the building blocks of building an actual PHP application. Once again, in real life, so many of these core components here, you won't have to build yourself. Somebody else will do them or a big group of people will contribute to building these things for you so that you never have to worry about it. And instead, you can just focus on building your site itself rather than the nitty gritty kind of boring details. But I hope you're learning some stuff.rather than the nitty gritty kind of boring details. But I hope you're learning some stuff. In the next episode, we'll move on.