So last time we were implementing the router class.
I'm not gonna lie, I think this was a complicated exercise, but the good news is that I also think
that this was the most complicated thing in the whole project.
That's why I wanted it out of the way as soon as possible.
So if you think that the whole project will be like that, we just made the worst part of the job,
the hardest part.
And now we've got a pretty nice system to build the app upon it.
First, let's see where we can use this router.
So we are creating the router class, the router object based on router class.
Inside this index.php file the main entry point to the app.
But we are not calling the dispatch method.
So we should do that first.
So we call router dispatch.
Now if we take a look at the dispatch method It accepts the string, URI and string method and it returns
a string.
So this means we are expected to get a response from this method that we should just echo.
That's why we're going to do it.
And again let's take a look at the arguments we need the URI and the method.
Where can we get this from.
So the URI we can get by using the parse URL function, to which we're going to pass something from
the super global.
So you can probably see how we are wrapping the super globals not to use them directly.
Instead, we are creating classes that provide this functionality in a more convenient way.
This is also what the major frameworks like Symfony or Laravel do.
So here we just need the request URI awry and the parse URL.
This returns an array.
It will return an associative array from which we need the path.
That's why we access it as an array immediately.
And it's nice that VSCode is suggesting what I can get out of that.
I need the path and the method.
So the method is also from this server super global.
And this is just a request method.
Okay.
We've got both.
We can pass that to the router.
But before we try this out we should define some routes because the router by default does not have
any routes.
So that's why we've got this inside the core section.
It's supposed to be flexible and universal.
Every app has different routes to define.
That's why we should create another file just for defining the routes.
So let's create a routes file for this project.
We can just keep it in the root directory.
But this doesn't really matter because what needs to happen is we need to tell the router what routes
do we have.
How we do that doesn't really matter.
That's secondary.
That's why I can put this file anywhere I want.
So that's just a PHP file that I'm going to include in my index file.
So maybe I'm gonna include it right after I create the router using require ones.
Relating to the current directory.
And I'm going to concatenate that with going up.
And there we have roots.
PHP.
Okay, so at this point where we get the URI and method and we dispatch, we already have the routes
defined because there are defined inside this file.
So now the router variable is available here.
That's just an object instance of this router class.
We know that it has the add method, but we don't really see it when we type the add method.
Because in this case in this context our IDE, the editor has no clue that we have this router variable.
And this is one of the problems when we just use the require or include functions of PHP, that it's
really hard for Ides and code editors to figure out where do we have this variable from, and what is
the possible type of that variable.
but to help ourselves we can use something called PHP docs.
It's been widely used to just let the editor know and maybe also other developers.
What is the type of a variable in a given context?
So this is looking like a comment, but it starts with two asterisks.
And right here in the middle I do an add sign and type var.
I'm going to just tell the editor what's the type of this variable.
And I'm just going to use the full namespace.
This is called router.
And after that I say that this applies to the router variable.
Keep in mind this is not code.
That's a comment.
But editors understand this.
And now if I use router and I try to type add, we can see this method listed with all the parameters
and return types should be much easier to use it.
So the method well, let's define with defining a route that has the get method for the URI.
That's just a forward slash.
That's our homepage.
And now the controller.
This would be home controller.
The action is index.
Now it might be useful to know a side note.
This is almost exactly how you would define routes in the Laravel framework.
So that's not something that you're just going to use in this course.
This is something that you would really do in the PHP development.
And by the way, I think it's also important or useful to know the ins and outs of how the frameworks
work.
Okay, let's add another route.
I might just copy paste this to be quicker.
So another get route might be posts where we're gonna list all the posts that we have.
It might use another class post controller, and also the action name would be index.
And then if we would like to see one post, one blog post, we're going to define another route.
This time we'll use the parameter.
The name of it would be ID and the action name would be show as in show me one post.
So before we run this, let's make sure the project is started.
And let's create those controllers.
First let's begin with the home controller PHP.
So the home controller how it should look like it's just a PHP file with a namespace.
Can you guess what's the namespace prefix?
We've seen that in Composer.json.
So this is app Up and it's followed by the folder structure.
So it should be next.
Controller.
And only then we define the class that's home controller.
The name of the class should match the file name except the extension.
Otherwise the autoloader would have trouble finding it.
And inside we add an action which is just a method called index.
Here we don't echo anything because remember what happens in the index file.
We echo the result of the dispatch method.
That's why we just need to return something.
Let it be just a text home.
Now again let's create another controller.
The post controller.
And I'm going to copy and paste the code for it to be quicker because the name changes here.
We also have an index action.
Let me call that all posts.
And another action will be called show.
Public function show.
And this one has a parameter the id.
So we can return the post number id.
So this would verify if we actually can create such class create such action, and if the router will
direct the specific URI to this action and will properly pass a parameter.
Okay, I think it's now finally time to test our router and controllers.
Remember that if something won't work, your first course of action should be checking the code because
there is always the code under every single video, all the changes are listed.
You should just check if your code is these identical.
That's most commonly the source of all the issues that there is a typo or something was misspelled.
So let's try it.
The main page is the home page looks fine.
Can the posts page work?
This also looks fine.
This just means that we are properly directed to the right controllers.
Now let me verify with some argument.
Maybe post with ID 15 or post with some text ID.
As you can see, this all works perfectly.
The proper page is displayed and the code is run properly and we are returning a response.
Now, finally, I think that we should take some time to just appreciate that even though the router
itself was probably a tiny bit complicated, especially if you haven't really programmed before.
Then the actual controllers and how hard it is now to create a new route.
So it's not hard at all.
It's super easy.
You just define everything in one single place, and you just create a class and a method and you return
something and if you need any parameters, they will be passed to your method.
I think this is super simple.
That's one of the reasons people love frameworks so much.
They really make a lot of things easier.
And you don't really have to write all of this yourself.
Now, if you are wondering why I am showing you all this, if there are frameworks and there are great
and you can use them, well, let me explain this.
So I think that you can be a decent developer by just using a framework, but if you want to be a great
developer Then it's really useful to understand how to solve different problems and how to solve hard
problems like the one we've just solved the routing, and it's always useful to know how things work
under the hood.
You really need to dive deeper to really understand things.
And if some of you are worried that I will replace you.
Well, I don't think it will replace the best of the best.
That's why I personally think you should always aim to be the best and know as much as possible, and
just try more to understand things deeper than other people.
Then you can never be replaced, because the truth is that most people are lazy or very lazy.
So if you have the curiosity and you are willing to put in the work to learn something in depth, you
already show everyone that you are irreplaceable.