So the first thing that we need to build into the new project is routing.
Can't really do anything without routes.
So let's remind ourselves how we built the previous router in the contact form project.
It was a simple file based routing.
You just had to create a specific file with specific name and HTTP method, and you got a route.
So we also had the dispatch method that converted the URIs into specific files that just needed to be
placed inside the specific directory.
That was everything.
Now we'd like some maybe not more complex, but more universal solution.
And we'd like to be more flexible in what routes we define.
We'd also like to accept parameters as a part of a route.
So first let me show you the diagram.
Super simple diagram of what do we expect, what we would like to do.
And we're gonna base that on the example.
So this is an example route I'd like to be able to define.
So for the HTTP method get I'd like a page that can display a specific blog post under slash or forward
slash posts.
Forward slash ID.
Now this would be marking a parameter in a route.
So this is the parameter name.
And you surround the parameter name with curly braces.
And this is what we expect to match by such route.
So this is post forward slash.
And that's the parameter value that we would like to read.
So we will just tell our router class what routes do we have?
And those routes need to match to controllers and actions.
So we're gonna be talking in details about that later.
For now, let me just explain how this routing should work.
So it is important that we're going to have a segment separator.
So the whole UI will be chunked into segments.
That's a segment separator.
That's why in the route definition we've got one segment called posts and another segment where we have
a parameter.
And the way we're going to match the URIs to the defined routes is first we're gonna compare if we have
the same segment count.
Now this would apply to all the defined routes until we find a matching one.
So for every route that we consider we're going to first check if the segment count is the same.
Here you can see it is the same.
We've got two segments.
Then we are either going to compare the segments literally.
Like in this case post is exactly matching posts.
Or if we encounter a curly brace, we are just going to read the parameter value because this is what
what is expected.
And essentially that's our way for matching routes.
Super simple.
But I think it will work great.
Okay, so I just wanted to explain that before we start.
So you know what our goals are and our goals are to create this routing class, this router class,
I will try to reuse as much code as possible from what we have created before, but we need to remember
that we are doing things a little bit differently.
And now we would like to use classes and objects.
Okay, now let's jump into the code editor.
Okay, so you might be asking yourself, why can't we just use the solution from the previous project?
It worked well.
Well, it did work well, but first we'd like to move to objects and classes second.
This solution is not really flexible.
I'd like to implement routing the way it works in the more modern frameworks like Laravel or Symphony.
And the third reason is we are here to learn.
So we need to see a lot of solutions and a lot of ways that we can solve the same problem on different
levels, not just building the specific projects.
So that's my reasoning at why we should be building things over and over again.
Now we're gonna reuse some code from this router so not everything will go to waste.
I just want to build a more flexible solution as we advance.
Okay, having this set, let me just scaffold first all the methods and things that this class needs.
And then we're going to be implementing that.
The first thing is having the field defined.
Let it be protected.
That's an array.
And it will just hold all the routes.
So it's an array.
You can add more routes.
There are not files.
You can be much more flexible.
You can even add routes at runtime.
And that's exactly what we'll be doing.
This means we need a function to add a route.
So it needs to accept a method.
Then it needs a URI.
And then finally it needs a controller.
It's void doesn't return anything.
Implementation will be added later.
Then we need the dispatch method.
It's public.
And this one would be well it would be having the same arguments as our previous implementation.
So we are only passing the URI and the method.
And this time we expect it to return a string.
Then we've got some internal methods for the router logic.
One of those is find route for the given URI and for the given method we need to return a specific route.
And the argument here is a nullable array.
Now it is a nullable array because if no route could be found, we return null and if a route can be
found, we return the passed parameters.
So let me quickly remind you how the roots can look like and how they are defined.
So we are writing the roots the way the URI looks like.
So we use the separator, but we have static parts like this posts.
This needs to just be matched literally.
This is what we literally expect in the URI like in this example here.
But then it can also have parameters and in case of parameters they are inside curly braces.
They have a name like ID in here.
And we just need to grab the value.
And every route has those segments separated by a forward slash.
This is an example of the identified segments.
And then we compare the count of segments and every individual segment.
So this is what will be happening here.
Either null mean meaning nothing can be matched or an array with all the parsed parameters, then we're
going to have another method.
This time it will be called match route.
So match route will be used inside the find route.
So this will be doing all the heavy lifting comparing the route URI with the actual request URI.
So remember the things that I have explained how this would behave on the diagram.
This function will be doing the heavy lifting comparing a specific two routes.
But remember that we can have many routes defined.
That's why this find route function will be running match route for every single route we have defined.
Unless we find a matching one and it returns the same type A Nullable array.
Then we have another method.
It's called call action.
It's just going to run the code if we match a specific route.
So controller is essentially a controller class.
And action is the method name.
So this would involve some dynamic class creation and dynamic method calling.
I think this would be pretty interesting.
For now let's assume that the response if is of type string then.
Additionally we're going to add a Notfound and a redirect methods.
But I think we can do that in the next videos after we implement the essentials here.
Let's now take a break, because I think there was a lot of theory and explaining of what we are about
to do.
Then next up, we need to actually go ahead and implement this logic.