WEBVTT

00:00.530 --> 00:06.140
Okay guys, next up we need to define the routes for this posts controller.

00:07.070 --> 00:13.580
So let me start by copy pasting the same fragment a couple of times.

00:13.670 --> 00:17.120
So let's make it this way.

00:17.720 --> 00:23.300
So we only have to fill out the action and the last part of the route.

00:23.480 --> 00:32.570
So this one would be for listing every single post, then a form for creating the post, then the form

00:32.570 --> 00:33.680
submission.

00:33.860 --> 00:37.760
That would be a post, then the edit form.

00:37.760 --> 00:39.770
So that's get.

00:39.950 --> 00:47.480
Then we've got the edit form submission and finally deleting of a post.

00:47.510 --> 00:56.120
Now in frameworks like Laravel, since if you stick to conventions of naming your controller actions,

00:56.120 --> 00:58.250
then it would be much simpler.

00:58.250 --> 01:07.000
You can use something like router resource and it will automatically assume some default routes and

01:07.000 --> 01:12.580
some default controller actions, which is super handy, and we might think about that later on.

01:12.610 --> 01:16.060
For now, we need to do some manual labor.

01:16.360 --> 01:19.720
So this first action is index.

01:19.750 --> 01:22.240
The second one is create.

01:22.240 --> 01:25.570
And also we need to change the route a little.

01:25.600 --> 01:30.610
Next up it's post and the action is store.

01:31.600 --> 01:33.430
Then we display the edit form.

01:33.430 --> 01:35.770
This needs an ID parameter.

01:36.370 --> 01:40.900
And that's the part of the path called edit.

01:40.930 --> 01:44.230
And the action it can also be called edit.

01:44.260 --> 01:47.380
Now the submission of this form.

01:48.730 --> 01:51.850
This just needs ID in the path.

01:51.850 --> 01:55.420
And the the name of the method is update.

01:55.420 --> 02:01.900
And finally to delete a post we can do it.

02:01.900 --> 02:02.590
This way.

02:02.620 --> 02:12.890
Normally we can just use a delete verb, but it's hard to submit a form with delete method.

02:12.920 --> 02:16.250
That's why we're going to add this specific path.

02:16.250 --> 02:19.880
And we're going to call the delete method.

02:19.910 --> 02:24.680
I think we've got every single route defined.

02:26.090 --> 02:30.290
Next up we should add a view to display those posts.

02:30.440 --> 02:34.010
So inside the views folder dashboard.

02:34.040 --> 02:36.590
Or maybe we should create another one.

02:36.590 --> 02:42.110
So this is posts or rather posts.

02:42.140 --> 02:43.970
Let's stick to this convention.

02:44.000 --> 02:45.740
Let me call that post.

02:45.770 --> 02:50.660
And let's begin by creating an index.php file.

02:52.850 --> 02:57.410
And in here we can say manage posts.

02:58.460 --> 03:00.680
Let me see if this works right away.

03:00.770 --> 03:04.490
Um well it should work.

03:04.490 --> 03:12.370
So let me jump to the post admin controller and let me see why this doesn't work.

03:12.400 --> 03:21.250
Okay, so we've assumed that the template name starts with posts, and it seems that we used the post

03:21.280 --> 03:22.720
convention before.

03:22.720 --> 03:24.340
So let me stick to it.

03:24.700 --> 03:28.180
And I think that's the only one.

03:28.210 --> 03:28.600
Okay.

03:28.630 --> 03:30.100
Now we have this page.

03:30.250 --> 03:31.780
So let me go back to it.

03:31.810 --> 03:33.220
Manage posts.

03:33.370 --> 03:39.190
We should have a link here that would be called create a New post.

03:39.940 --> 03:42.280
This is Create New.

03:44.440 --> 03:53.410
Post and it's going to lead to admin post create.

03:56.500 --> 03:59.860
Well template doesn't exist so that was expected.

03:59.860 --> 04:02.680
And now let's just use a table.

04:02.680 --> 04:07.150
I think it might suit our situation well.

04:07.150 --> 04:12.080
Let me add a head section on the table and one row.

04:12.410 --> 04:15.050
And now sorry that's not TD.

04:15.260 --> 04:16.850
It is th.

04:16.880 --> 04:21.050
In the table header we're going to have a title.

04:21.920 --> 04:27.170
Then another one would be when a post was created.

04:27.170 --> 04:32.810
And then finally we're going to have some actions to do.

04:36.530 --> 04:37.070
Okay.

04:37.070 --> 04:38.420
So it looks like this.

04:38.420 --> 04:43.940
And then after the t head section let's add a T body.

04:44.060 --> 04:49.010
Now this is where we're going to display all the posts.

04:49.040 --> 04:55.130
That's why we're going to use for each posts as post.

04:55.910 --> 05:06.440
And I'm going to close this with and for each inside every single one we display a row and the values.

05:08.150 --> 05:15.250
So we're going to use HTML special chars as usual to display the post title.

05:18.970 --> 05:26.350
The next column will just display the post created.

05:28.840 --> 05:30.040
At date.

05:36.670 --> 05:38.350
And the last one.

05:39.430 --> 05:41.020
This would be actions.

05:41.170 --> 05:43.570
So we've got a couple of those actions in here.

05:43.570 --> 05:45.670
Let me move to another row.

05:46.930 --> 05:56.170
So the one is to edit this would be admin posts post ID slash edit.

05:57.190 --> 06:01.720
And this can be just a link.

06:02.920 --> 06:06.070
But then the next action is more destructive.

06:06.070 --> 06:07.540
It's deleting a post.

06:07.540 --> 06:10.330
That's why we need a form for that.

06:10.330 --> 06:12.280
And the action here.

06:12.280 --> 06:27.630
This is admin posts Again, we need the post ID and then it is delete and the method here is post.

06:30.720 --> 06:37.200
And inside we just need a button that will say delete.

06:39.360 --> 06:42.570
The type of it is submit.

06:45.750 --> 06:49.560
And we might add a tiny bit of JavaScript in here.

06:49.890 --> 06:52.170
Just a simple confirmation.

06:54.780 --> 06:57.360
So we're going to do return confirm.

06:57.360 --> 07:05.430
And we're going to ask the user if he really wants to delete a post by asking are you sure.

07:09.420 --> 07:10.020
Okay.

07:10.020 --> 07:13.680
So maybe let's do a quick test.

07:13.920 --> 07:18.150
Let's try to delete one of those blog posts, maybe this one.

07:18.160 --> 07:21.670
So we are being asked if we really want to do that.

07:21.670 --> 07:24.490
If I say cancel, well nothing happened.

07:24.520 --> 07:28.990
I have refreshed the page and the blog post is still there.

07:29.080 --> 07:36.550
But if I go ahead with okay, um, well, we've got an expired page.

07:36.700 --> 07:40.150
So as you see, the protection by default works fine.

07:40.150 --> 07:43.570
That's why we need the CSRF token.

07:44.530 --> 07:46.600
And now let me try again.

07:46.930 --> 07:49.180
Let me try to remove this post.

07:49.180 --> 07:50.740
And there we have it.

07:50.770 --> 07:56.440
One of our free posts was just removed and it's gone forever.

07:57.220 --> 07:59.920
So this worked fine now.

07:59.950 --> 08:05.380
Well we are missing the templates for editing, so next up we should be adding them.

08:05.380 --> 08:08.200
But for now we've got the list of posts.

08:08.380 --> 08:17.080
We've got the delete button working, and we need to still work on adding the post and editing, which

08:17.080 --> 08:21.400
I think we can do next in the next videos.
