WEBVTT

00:00.080 --> 00:03.290
So last time we were implementing the router class.

00:03.290 --> 00:10.400
I'm not gonna lie, I think this was a complicated exercise, but the good news is that I also think

00:10.400 --> 00:14.300
that this was the most complicated thing in the whole project.

00:14.300 --> 00:18.680
That's why I wanted it out of the way as soon as possible.

00:18.710 --> 00:26.090
So if you think that the whole project will be like that, we just made the worst part of the job,

00:26.090 --> 00:27.170
the hardest part.

00:27.170 --> 00:32.690
And now we've got a pretty nice system to build the app upon it.

00:32.960 --> 00:37.580
First, let's see where we can use this router.

00:37.580 --> 00:42.320
So we are creating the router class, the router object based on router class.

00:42.320 --> 00:47.540
Inside this index.php file the main entry point to the app.

00:47.570 --> 00:51.230
But we are not calling the dispatch method.

00:51.230 --> 00:53.120
So we should do that first.

00:53.120 --> 00:57.050
So we call router dispatch.

00:57.080 --> 01:05.740
Now if we take a look at the dispatch method It accepts the string, URI and string method and it returns

01:05.770 --> 01:06.730
a string.

01:06.760 --> 01:13.930
So this means we are expected to get a response from this method that we should just echo.

01:13.960 --> 01:15.970
That's why we're going to do it.

01:16.480 --> 01:21.730
And again let's take a look at the arguments we need the URI and the method.

01:21.760 --> 01:24.010
Where can we get this from.

01:25.000 --> 01:32.470
So the URI we can get by using the parse URL function, to which we're going to pass something from

01:32.470 --> 01:33.760
the super global.

01:33.880 --> 01:40.210
So you can probably see how we are wrapping the super globals not to use them directly.

01:40.210 --> 01:46.900
Instead, we are creating classes that provide this functionality in a more convenient way.

01:46.900 --> 01:52.870
This is also what the major frameworks like Symfony or Laravel do.

01:54.580 --> 02:01.090
So here we just need the request URI awry and the parse URL.

02:01.120 --> 02:02.680
This returns an array.

02:03.490 --> 02:08.170
It will return an associative array from which we need the path.

02:08.200 --> 02:11.200
That's why we access it as an array immediately.

02:11.200 --> 02:16.210
And it's nice that VSCode is suggesting what I can get out of that.

02:16.210 --> 02:21.550
I need the path and the method.

02:21.820 --> 02:27.670
So the method is also from this server super global.

02:28.750 --> 02:33.160
And this is just a request method.

02:33.670 --> 02:34.360
Okay.

02:34.360 --> 02:35.590
We've got both.

02:35.590 --> 02:38.080
We can pass that to the router.

02:40.000 --> 02:49.240
But before we try this out we should define some routes because the router by default does not have

02:49.240 --> 02:49.810
any routes.

02:49.810 --> 02:54.490
So that's why we've got this inside the core section.

02:54.490 --> 02:57.310
It's supposed to be flexible and universal.

02:57.310 --> 03:00.210
Every app has different routes to define.

03:00.210 --> 03:05.880
That's why we should create another file just for defining the routes.

03:07.260 --> 03:11.730
So let's create a routes file for this project.

03:12.480 --> 03:15.360
We can just keep it in the root directory.

03:15.360 --> 03:22.410
But this doesn't really matter because what needs to happen is we need to tell the router what routes

03:22.410 --> 03:23.370
do we have.

03:23.400 --> 03:26.040
How we do that doesn't really matter.

03:26.040 --> 03:27.630
That's secondary.

03:27.660 --> 03:31.890
That's why I can put this file anywhere I want.

03:31.920 --> 03:37.680
So that's just a PHP file that I'm going to include in my index file.

03:37.800 --> 03:45.870
So maybe I'm gonna include it right after I create the router using require ones.

03:48.360 --> 03:51.660
Relating to the current directory.

03:51.660 --> 03:55.920
And I'm going to concatenate that with going up.

03:56.400 --> 03:57.900
And there we have roots.

03:58.230 --> 03:58.620
PHP.

04:01.320 --> 04:07.530
Okay, so at this point where we get the URI and method and we dispatch, we already have the routes

04:07.530 --> 04:11.910
defined because there are defined inside this file.

04:11.940 --> 04:14.880
So now the router variable is available here.

04:14.880 --> 04:19.080
That's just an object instance of this router class.

04:19.080 --> 04:25.440
We know that it has the add method, but we don't really see it when we type the add method.

04:25.470 --> 04:34.290
Because in this case in this context our IDE, the editor has no clue that we have this router variable.

04:34.290 --> 04:45.270
And this is one of the problems when we just use the require or include functions of PHP, that it's

04:45.270 --> 04:53.790
really hard for Ides and code editors to figure out where do we have this variable from, and what is

04:53.790 --> 04:56.160
the possible type of that variable.

04:56.210 --> 05:00.860
but to help ourselves we can use something called PHP docs.

05:00.860 --> 05:07.910
It's been widely used to just let the editor know and maybe also other developers.

05:07.910 --> 05:11.480
What is the type of a variable in a given context?

05:11.480 --> 05:17.390
So this is looking like a comment, but it starts with two asterisks.

05:17.390 --> 05:24.680
And right here in the middle I do an add sign and type var.

05:24.710 --> 05:28.190
I'm going to just tell the editor what's the type of this variable.

05:28.190 --> 05:30.650
And I'm just going to use the full namespace.

05:30.650 --> 05:32.390
This is called router.

05:32.480 --> 05:36.920
And after that I say that this applies to the router variable.

05:36.950 --> 05:39.440
Keep in mind this is not code.

05:39.440 --> 05:40.550
That's a comment.

05:40.550 --> 05:43.040
But editors understand this.

05:43.040 --> 05:52.640
And now if I use router and I try to type add, we can see this method listed with all the parameters

05:52.640 --> 05:56.890
and return types should be much easier to use it.

05:56.890 --> 06:04.090
So the method well, let's define with defining a route that has the get method for the URI.

06:04.150 --> 06:06.850
That's just a forward slash.

06:06.850 --> 06:08.440
That's our homepage.

06:08.440 --> 06:09.940
And now the controller.

06:09.970 --> 06:13.060
This would be home controller.

06:13.420 --> 06:15.880
The action is index.

06:15.910 --> 06:20.020
Now it might be useful to know a side note.

06:20.020 --> 06:25.930
This is almost exactly how you would define routes in the Laravel framework.

06:25.930 --> 06:30.580
So that's not something that you're just going to use in this course.

06:30.580 --> 06:35.500
This is something that you would really do in the PHP development.

06:35.500 --> 06:44.530
And by the way, I think it's also important or useful to know the ins and outs of how the frameworks

06:44.530 --> 06:45.490
work.

06:46.300 --> 06:48.340
Okay, let's add another route.

06:48.340 --> 06:51.430
I might just copy paste this to be quicker.

06:51.550 --> 06:58.150
So another get route might be posts where we're gonna list all the posts that we have.

06:58.180 --> 07:04.270
It might use another class post controller, and also the action name would be index.

07:04.270 --> 07:11.410
And then if we would like to see one post, one blog post, we're going to define another route.

07:11.410 --> 07:13.660
This time we'll use the parameter.

07:13.660 --> 07:22.270
The name of it would be ID and the action name would be show as in show me one post.

07:23.200 --> 07:27.940
So before we run this, let's make sure the project is started.

07:31.180 --> 07:33.250
And let's create those controllers.

07:33.250 --> 07:37.750
First let's begin with the home controller PHP.

07:38.500 --> 07:45.460
So the home controller how it should look like it's just a PHP file with a namespace.

07:45.490 --> 07:47.980
Can you guess what's the namespace prefix?

07:47.980 --> 07:50.410
We've seen that in Composer.json.

07:51.040 --> 07:57.540
So this is app Up and it's followed by the folder structure.

07:57.540 --> 07:59.160
So it should be next.

07:59.220 --> 08:00.240
Controller.

08:00.240 --> 08:04.050
And only then we define the class that's home controller.

08:04.080 --> 08:09.270
The name of the class should match the file name except the extension.

08:10.140 --> 08:13.950
Otherwise the autoloader would have trouble finding it.

08:13.980 --> 08:20.490
And inside we add an action which is just a method called index.

08:20.610 --> 08:27.660
Here we don't echo anything because remember what happens in the index file.

08:27.750 --> 08:32.190
We echo the result of the dispatch method.

08:32.220 --> 08:35.100
That's why we just need to return something.

08:35.580 --> 08:38.850
Let it be just a text home.

08:39.360 --> 08:42.270
Now again let's create another controller.

08:42.300 --> 08:45.870
The post controller.

08:48.840 --> 08:54.320
And I'm going to copy and paste the code for it to be quicker because the name changes here.

08:54.320 --> 08:56.390
We also have an index action.

08:56.390 --> 08:58.940
Let me call that all posts.

08:59.720 --> 09:03.260
And another action will be called show.

09:03.590 --> 09:05.570
Public function show.

09:05.570 --> 09:08.630
And this one has a parameter the id.

09:11.180 --> 09:14.930
So we can return the post number id.

09:15.380 --> 09:23.660
So this would verify if we actually can create such class create such action, and if the router will

09:23.660 --> 09:29.900
direct the specific URI to this action and will properly pass a parameter.

09:30.230 --> 09:35.780
Okay, I think it's now finally time to test our router and controllers.

09:35.780 --> 09:42.350
Remember that if something won't work, your first course of action should be checking the code because

09:42.350 --> 09:47.750
there is always the code under every single video, all the changes are listed.

09:47.840 --> 09:51.530
You should just check if your code is these identical.

09:51.530 --> 09:59.630
That's most commonly the source of all the issues that there is a typo or something was misspelled.

10:01.130 --> 10:02.600
So let's try it.

10:02.600 --> 10:06.200
The main page is the home page looks fine.

10:07.010 --> 10:09.470
Can the posts page work?

10:10.190 --> 10:11.690
This also looks fine.

10:11.690 --> 10:17.330
This just means that we are properly directed to the right controllers.

10:17.360 --> 10:20.030
Now let me verify with some argument.

10:20.210 --> 10:25.730
Maybe post with ID 15 or post with some text ID.

10:26.360 --> 10:29.990
As you can see, this all works perfectly.

10:29.990 --> 10:39.410
The proper page is displayed and the code is run properly and we are returning a response.

10:39.980 --> 10:45.710
Now, finally, I think that we should take some time to just appreciate that even though the router

10:45.710 --> 10:53.810
itself was probably a tiny bit complicated, especially if you haven't really programmed before.

10:54.980 --> 11:01.190
Then the actual controllers and how hard it is now to create a new route.

11:01.190 --> 11:02.660
So it's not hard at all.

11:02.660 --> 11:03.890
It's super easy.

11:03.920 --> 11:12.050
You just define everything in one single place, and you just create a class and a method and you return

11:12.050 --> 11:17.510
something and if you need any parameters, they will be passed to your method.

11:17.540 --> 11:19.430
I think this is super simple.

11:19.430 --> 11:23.450
That's one of the reasons people love frameworks so much.

11:23.450 --> 11:26.450
They really make a lot of things easier.

11:26.450 --> 11:30.680
And you don't really have to write all of this yourself.

11:32.000 --> 11:37.670
Now, if you are wondering why I am showing you all this, if there are frameworks and there are great

11:37.670 --> 11:41.090
and you can use them, well, let me explain this.

11:41.090 --> 11:48.110
So I think that you can be a decent developer by just using a framework, but if you want to be a great

11:48.110 --> 11:55.510
developer Then it's really useful to understand how to solve different problems and how to solve hard

11:55.510 --> 12:04.750
problems like the one we've just solved the routing, and it's always useful to know how things work

12:04.750 --> 12:05.560
under the hood.

12:05.590 --> 12:09.490
You really need to dive deeper to really understand things.

12:09.490 --> 12:14.170
And if some of you are worried that I will replace you.

12:14.200 --> 12:18.040
Well, I don't think it will replace the best of the best.

12:18.040 --> 12:26.170
That's why I personally think you should always aim to be the best and know as much as possible, and

12:26.170 --> 12:30.970
just try more to understand things deeper than other people.

12:30.970 --> 12:39.280
Then you can never be replaced, because the truth is that most people are lazy or very lazy.

12:39.280 --> 12:46.150
So if you have the curiosity and you are willing to put in the work to learn something in depth, you

12:46.150 --> 12:49.000
already show everyone that you are irreplaceable.
