WEBVTT

00:00.380 --> 00:07.400
So a pretty useful feature to have in an application that lets users authenticate is to also create

00:07.400 --> 00:08.270
an account.

00:08.300 --> 00:13.760
Now we don't have this feature yet, and in the next couple of videos I'd like us to create it.

00:14.090 --> 00:23.000
So it would be actually pretty similar to the login feature, and it would be implemented in a similar

00:23.000 --> 00:23.750
way.

00:24.380 --> 00:31.610
So we already have controllers in our framework which does render individual pages or just handle logic

00:31.610 --> 00:36.440
of certain URLs which we can define inside routes.

00:37.340 --> 00:44.960
So as you see inside the auth controller which is responsible for signing users in, we typically have

00:44.960 --> 00:48.980
an action called create that displays a form.

00:49.280 --> 00:50.750
And that's everything it does.

00:50.750 --> 00:57.920
It displays the form and the form is submitted into another action of that controller.

00:57.920 --> 01:00.560
Typically we call this one store.

01:00.770 --> 01:09.890
This is the way we organize resources inside a single controller, so this one is responsible for signing

01:09.890 --> 01:10.820
users in.

01:10.850 --> 01:12.140
Nothing else.

01:12.170 --> 01:21.290
If we create a similar one that is responsible for creating accounts, we can reuse the same schema

01:21.290 --> 01:22.490
of thinking.

01:22.640 --> 01:30.770
So we can have a create action that displays a registration form, and then a store action that can

01:30.770 --> 01:40.340
create a user if some of the conditions are met, which can be that the email doesn't exist in the database,

01:40.340 --> 01:44.870
and that the password and repeated passwords are the same.

01:44.870 --> 01:49.160
For that, we can even reuse the form that we already have.

01:49.190 --> 01:53.240
I mean the HTML of the sign in form.

01:53.240 --> 01:56.300
So without further ado, let's get started.

01:56.300 --> 02:01.460
And let's go ahead and start implementing the registration logic.

02:02.450 --> 02:09.610
So to make matters quicker, let's just copy and paste this auth controller because we're going to reuse

02:09.610 --> 02:11.500
some concepts from it.

02:12.820 --> 02:20.170
So I'm going to paste it and I'm going to call it user controller as actually we're going to be working

02:20.170 --> 02:22.270
with the user model here.

02:24.190 --> 02:25.720
So I think it fits.

02:25.750 --> 02:30.340
Now let me jump to a concept that I've mentioned before.

02:30.340 --> 02:35.920
So I really think that there are a lot of great concepts in the Laravel framework.

02:36.610 --> 02:43.990
One of those is to create so-called resource controllers, which essentially means that you are assuming

02:43.990 --> 02:51.520
that when you are creating a controller class, which as you see, we are doing all the time, every

02:51.520 --> 02:56.440
single controller should just strive to work with one resource.

02:56.470 --> 03:04.720
What I mean by that, and what Laravel means by that is, for example, in this, uh, that we see right

03:04.750 --> 03:07.780
on the screen, we would like to work with photos.

03:07.780 --> 03:15.760
So we create a controller that can list a Photos, create a new one or display a form, store it into

03:15.760 --> 03:21.820
the database, display one photo, edit it, update it, and destroyed.

03:21.850 --> 03:24.340
Edit means display the form.

03:24.940 --> 03:34.330
So by sticking to this simple convention, we just make things simpler for ourselves and for other developers

03:34.330 --> 03:38.350
that might be working on the same project in the future.

03:38.500 --> 03:43.030
Because you never, ever would have to think about the action names.

03:43.030 --> 03:47.710
You would not have to think what someone meant and why.

03:47.710 --> 03:57.100
This action is created and called the way it is, because you would be expecting that you only see specific

03:57.100 --> 04:02.590
action names like create, and you instantly know what it's going to do.

04:02.590 --> 04:04.390
It's going to display a form.

04:04.390 --> 04:11.890
When you see a star action name, you know that it will store something in some kind of a database.

04:12.130 --> 04:16.860
And then you know that this controller is about users.

04:16.860 --> 04:24.540
So then you also instantly know it's going to store some user model inside the database table.

04:24.570 --> 04:32.190
I think that such conventions are super strong, and they just make you stop thinking about things that

04:32.190 --> 04:38.190
aren't important and focus on the most important things, which is actually building the app.

04:38.430 --> 04:47.430
So let's follow this path, and let's use this convention to quickly create the user controller for

04:47.430 --> 04:48.720
user registration.

04:49.740 --> 04:58.620
Now the part of the convention is also to call the folder that contains the views same as the controller.

04:58.650 --> 05:04.500
That's why I'm going to create a new folder and I will call it user.

05:05.310 --> 05:14.310
Inside it we need a form so we can quickly copy and paste this create form, because the registration

05:14.310 --> 05:18.180
form will have very similar fields to the login one.

05:20.760 --> 05:27.390
Now let's make sure that here I am rendering the user create form.

05:29.550 --> 05:34.380
And then let's also jump to routes and register some new routes.

05:35.520 --> 05:39.360
So we need two routes to display the registration form.

05:39.360 --> 05:42.480
And to actually handle the registration.

05:42.960 --> 05:49.590
Let me add them in the general routes and I might call it register.

05:53.130 --> 05:59.190
And the same path would be used for the post action because it contains different verb.

05:59.190 --> 06:02.430
So basically they are different.

06:02.580 --> 06:08.340
Now that's a user controller not an orphan.

06:08.340 --> 06:10.170
And there we have it.

06:10.200 --> 06:12.690
We should have this controller working.

06:12.720 --> 06:15.450
At least it should display something.

06:15.480 --> 06:19.110
So let's make sure that we have this create form.

06:19.110 --> 06:21.000
That's for the registration.

06:21.000 --> 06:29.750
And now let me call it maybe create count or maybe just register.

06:29.780 --> 06:31.640
Let's keep it simple.

06:31.850 --> 06:37.460
Okay, now I should be able to jump to our page.

06:37.520 --> 06:42.860
And if I type the URL manually, let it be Register.

06:43.100 --> 06:46.100
Now I'm seeing the registration form.

06:46.100 --> 06:51.500
Now we're gonna work from here because we need to add some more fields.

06:51.500 --> 06:55.940
When you sign in, you only supply the email and password.

06:55.940 --> 07:05.180
But when you register, you also have to provide your full name and you need to make sure you are repeating

07:05.180 --> 07:10.430
the password, and that is that it is also the same as the original password.

07:10.580 --> 07:13.820
So we're going to work on this in the next video.

07:13.850 --> 07:22.670
For now we've just created another controller and well another template and explained a little the way

07:22.670 --> 07:24.980
we're gonna work with this project.
