So let's start building the project.
The first choice that we have to make is what, to begin with, the list of things that we need to include
in the project might look long and intimidating, but we are going to approach everything step by step,
and I'm going to guide you through the process so you don't need to feel intimidated.
So I think that the first sensible step to begin with is to start with routing.
Now, obviously, I'm not going to go through every single topic in detail because I'd like us to see
the effects of what we do as soon as possible.
So we are going to implement enough routing so that we can get to work with something else.
So let's remind ourselves, what do we mean by routing?
I'm going to zoom in now.
We need this main file, the front controller, the entry point to our application that can distribute
the traffic to different PHP files that we can call route handlers depending on the request method.
Thus either get or post and the URL like slash contact or post slash contact or slash guestbook.
Let's jump to the code editor and start implementing this right away.
So let's start by creating the public folder.
And another one it would be called includes, and it will contain all the common libraries that we have
for our app.
Now inside the public folder we create the front controller, the entry point to our app the index.php
file.
Now I'd like to use strict types.
That's why I begin by adding declare strict types equals one.
So this index file is the entry point of our app.
Now every single request coming to our application will go through this specific index.php file.
So maybe this is a good place to start a session.
So let me start the session because we're gonna be relying on sessions in here.
Another thing that we can do here is we can define some constants, like the directory names, the absolute
paths to all the directories that we're going to use inside the app.
Like what is the root directory.
The templates directory and includes directory so that later on we won't have to add relative paths
to those directories, which might be cumbersome.
So let me define the first one the includes directory.
And in PHP we've got this underscore underscore dir constant which will always contain the actual directory.
In this case this would be the public directory.
And I concatenate that with slash two dots which means go one directory up.
So here we're going to be in the root directory and slash includes.
Now, this constant would be available in every single PHP file of our project, because I've declared
that in the index PHP file, which would then include all subsequent route handlers.
So now let's add the file that will contain the routing logic.
Let's call this router PHP and create this file inside the includes folder.
Let me add the PHP tag and go back to the front controller.
Now that I have this includes directory defined as a constant.
I can simply include this file by using require.
Once right after I start the session I can use this includes directory and pass the router.
PHP.
Now the next thing that we should do here is to handle the request by calling some function of the router.
So that means we need to jump to the router.
PHP.
And here we're gonna be creating the logic that is handling the routing.
First let's add the strict type declaration.
So it needs to be in every single PHP file.
So that's the first thing.
Now let me create the function.
So I'm going to call it dispatch.
As we are dispatching the request to a specific department that knows how to handle it.
It makes sense.
And I'd like it to have two arguments.
The first is the URI.
So that's the part of the URL without the domain.
So we know where someone needs to go.
Where do we send this guy.
And another thing that we need is the method.
This means that those two values would have to be passed to this function.
It doesn't return any value.
And this is because this function is returning the response from our script.
It won't be consumed by PHP anymore.
So the return type here does not matter.
Now let me list the things that need to happen in this dispatch function.
First off, we need to normalize Allies, the URI, and this is because we need to match it to a file
name that we're going to have.
Let me give you an example.
If someone will go to this URI and we'll use the Get method.
So that's the default method.
When you just visit a page in a browser, we need a way to match it to a file name.
And we're going to keep this route handlers inside the routes directory.
And there must be a file that will be called guestbook underscore.
Get PHP and we need those two parts guestbook to match.
And in PHP you might remember that the case of the letters matter.
So this is not a complex thing, but we just need to make sure that we are able to find this file.
Now the second thing we need to make sure that we support the given HTTP method because there is more
than just get and post, you've got put, patch, delete, etc. and we support only those two.
So if the method is different we just return 404.
So not found not supported.
The third step is converting the URI and the method to the actual file path.
The PHP file path.
The route handler that we are about to include here so it can handle the request.
And then the fourth step is to check if this file exists.
If not, we return 404 that the page wasn't found.
And if it exists then handle the route by including the PHP file.
And that's it.
That's all the things that we have to do.
So at this point, let's just take a short break and we're going to go back and in the next video implement
all those five steps.