WEBVTT

00:00.290 --> 00:02.510
So let's start building the project.

00:02.510 --> 00:08.810
The first choice that we have to make is what, to begin with, the list of things that we need to include

00:08.810 --> 00:17.390
in the project might look long and intimidating, but we are going to approach everything step by step,

00:17.390 --> 00:22.700
and I'm going to guide you through the process so you don't need to feel intimidated.

00:22.700 --> 00:29.030
So I think that the first sensible step to begin with is to start with routing.

00:29.030 --> 00:36.680
Now, obviously, I'm not going to go through every single topic in detail because I'd like us to see

00:36.680 --> 00:39.980
the effects of what we do as soon as possible.

00:39.980 --> 00:47.540
So we are going to implement enough routing so that we can get to work with something else.

00:47.540 --> 00:51.440
So let's remind ourselves, what do we mean by routing?

00:51.440 --> 00:52.970
I'm going to zoom in now.

00:52.970 --> 01:00.110
We need this main file, the front controller, the entry point to our application that can distribute

01:00.110 --> 01:08.810
the traffic to different PHP files that we can call route handlers depending on the request method.

01:08.810 --> 01:17.120
Thus either get or post and the URL like slash contact or post slash contact or slash guestbook.

01:17.510 --> 01:22.910
Let's jump to the code editor and start implementing this right away.

01:23.660 --> 01:27.410
So let's start by creating the public folder.

01:27.410 --> 01:34.820
And another one it would be called includes, and it will contain all the common libraries that we have

01:34.850 --> 01:35.990
for our app.

01:36.020 --> 01:42.680
Now inside the public folder we create the front controller, the entry point to our app the index.php

01:42.680 --> 01:43.250
file.

01:43.280 --> 01:46.190
Now I'd like to use strict types.

01:46.190 --> 01:52.850
That's why I begin by adding declare strict types equals one.

01:54.170 --> 01:57.920
So this index file is the entry point of our app.

01:57.950 --> 02:04.430
Now every single request coming to our application will go through this specific index.php file.

02:04.430 --> 02:09.020
So maybe this is a good place to start a session.

02:09.200 --> 02:14.660
So let me start the session because we're gonna be relying on sessions in here.

02:14.690 --> 02:23.900
Another thing that we can do here is we can define some constants, like the directory names, the absolute

02:23.900 --> 02:29.000
paths to all the directories that we're going to use inside the app.

02:29.000 --> 02:31.850
Like what is the root directory.

02:31.850 --> 02:39.920
The templates directory and includes directory so that later on we won't have to add relative paths

02:39.920 --> 02:43.520
to those directories, which might be cumbersome.

02:43.520 --> 02:47.810
So let me define the first one the includes directory.

02:47.810 --> 02:56.810
And in PHP we've got this underscore underscore dir constant which will always contain the actual directory.

02:56.810 --> 02:59.390
In this case this would be the public directory.

02:59.390 --> 03:08.330
And I concatenate that with slash two dots which means go one directory up.

03:08.330 --> 03:14.480
So here we're going to be in the root directory and slash includes.

03:14.510 --> 03:21.740
Now, this constant would be available in every single PHP file of our project, because I've declared

03:21.740 --> 03:28.730
that in the index PHP file, which would then include all subsequent route handlers.

03:29.750 --> 03:34.040
So now let's add the file that will contain the routing logic.

03:34.040 --> 03:39.440
Let's call this router PHP and create this file inside the includes folder.

03:39.470 --> 03:43.280
Let me add the PHP tag and go back to the front controller.

03:43.310 --> 03:47.450
Now that I have this includes directory defined as a constant.

03:47.450 --> 03:51.770
I can simply include this file by using require.

03:51.800 --> 04:00.290
Once right after I start the session I can use this includes directory and pass the router.

04:00.560 --> 04:00.920
PHP.

04:01.220 --> 04:09.290
Now the next thing that we should do here is to handle the request by calling some function of the router.

04:09.290 --> 04:11.720
So that means we need to jump to the router.

04:11.900 --> 04:12.260
PHP.

04:12.260 --> 04:16.870
And here we're gonna be creating the logic that is handling the routing.

04:16.870 --> 04:20.320
First let's add the strict type declaration.

04:20.320 --> 04:23.500
So it needs to be in every single PHP file.

04:24.370 --> 04:25.780
So that's the first thing.

04:25.780 --> 04:28.390
Now let me create the function.

04:28.390 --> 04:30.580
So I'm going to call it dispatch.

04:30.610 --> 04:36.400
As we are dispatching the request to a specific department that knows how to handle it.

04:36.430 --> 04:37.450
It makes sense.

04:37.450 --> 04:39.790
And I'd like it to have two arguments.

04:39.790 --> 04:41.680
The first is the URI.

04:41.710 --> 04:45.640
So that's the part of the URL without the domain.

04:45.640 --> 04:47.920
So we know where someone needs to go.

04:47.950 --> 04:49.540
Where do we send this guy.

04:49.540 --> 04:52.510
And another thing that we need is the method.

04:52.510 --> 04:56.680
This means that those two values would have to be passed to this function.

04:56.680 --> 04:59.020
It doesn't return any value.

04:59.860 --> 05:05.830
And this is because this function is returning the response from our script.

05:05.830 --> 05:08.470
It won't be consumed by PHP anymore.

05:08.470 --> 05:11.680
So the return type here does not matter.

05:11.710 --> 05:16.210
Now let me list the things that need to happen in this dispatch function.

05:16.210 --> 05:25.270
First off, we need to normalize Allies, the URI, and this is because we need to match it to a file

05:25.300 --> 05:27.040
name that we're going to have.

05:27.040 --> 05:28.540
Let me give you an example.

05:28.540 --> 05:34.630
If someone will go to this URI and we'll use the Get method.

05:34.630 --> 05:36.250
So that's the default method.

05:36.250 --> 05:41.680
When you just visit a page in a browser, we need a way to match it to a file name.

05:41.680 --> 05:46.930
And we're going to keep this route handlers inside the routes directory.

05:46.930 --> 05:52.180
And there must be a file that will be called guestbook underscore.

05:52.210 --> 05:57.910
Get PHP and we need those two parts guestbook to match.

05:57.910 --> 06:03.820
And in PHP you might remember that the case of the letters matter.

06:03.820 --> 06:11.290
So this is not a complex thing, but we just need to make sure that we are able to find this file.

06:13.180 --> 06:20.440
Now the second thing we need to make sure that we support the given HTTP method because there is more

06:20.440 --> 06:28.300
than just get and post, you've got put, patch, delete, etc. and we support only those two.

06:28.330 --> 06:34.240
So if the method is different we just return 404.

06:34.240 --> 06:37.030
So not found not supported.

06:37.210 --> 06:44.770
The third step is converting the URI and the method to the actual file path.

06:45.220 --> 06:47.380
The PHP file path.

06:47.380 --> 06:52.300
The route handler that we are about to include here so it can handle the request.

06:52.300 --> 06:57.910
And then the fourth step is to check if this file exists.

06:58.480 --> 07:03.730
If not, we return 404 that the page wasn't found.

07:06.040 --> 07:14.650
And if it exists then handle the route by including the PHP file.

07:14.650 --> 07:15.430
And that's it.

07:15.430 --> 07:18.190
That's all the things that we have to do.

07:19.060 --> 07:25.750
So at this point, let's just take a short break and we're going to go back and in the next video implement

07:25.750 --> 07:28.000
all those five steps.
