This episode is for members only

Sign up to access "Build Your Own PHP Framework" right now.

Get started
Already a member? Sign in to continue
Playing
30. Creating the registration flow

Episodes

0%
Your progress
  • Total: 4h 45m
  • Played: 0m
  • Remaining: 4h 45m
Join or sign in to track your progress

Transcript

00:00
Before we actually register a user, let's take a look at the registration flow, creating the controller in the form,
00:06
and posting data through to the back end. OK, so we're going to start out by just creating out a new controller. So let's go over to the HTTP section.
00:16
Under our controllers, let's create our new directory just to keep these nice and separate, and we'll call that auth. So there's a couple of ways that we can do this.
00:24
We could create one controller and have two methods in it, or we could create two separate controllers. Let's just create one controller, just so I can show you how to register these
00:34
in your web routes, and then feel free to break these up if you want to. OK, let's go ahead and create out a, just a standard file in here,
00:41
and we'll copy one of our other controllers over. And of course, we are going to call this register controller. So let's just grab any of our other controllers that we've got in here, and just paste that in.
00:52
And let's switch over the dashboard controller here to register controller. And of course, we forgot to give this a PHP extension. So let's do that now.
01:01
OK, so inside of our register controller, we've got an invoke magic method at the moment, which we were using previously. Let's create out an index method,
01:09
and then a store method, which will be responsible for actually registering the user. So here, we're going to go into an auth directory, and we're going to render out register.twig.
01:19
Let's create that out now, and at least so we can see something on the page. So over in our views, let's create a directory called auth. Inside of here, let's create out register.twig.
01:30
And once again, we'll just copy over the layout from one of our other pages. Let's paste this in. Let's change the title of this,
01:37
and we'll just add the header as register. And we'll be building out our form directly in here. OK, now that we've done this, let's just register the route. So over to our web routes,
01:47
and let's create this out down here. So let's say router, get, and let's go and point this to register. And now here, we're going to put this in an array,
01:59
so we have the method as the callable thing that we want to render rather than the entire controller. So we'll reference our register controller in here, and then we'll just reference that index method,
02:10
and that will work as normal. OK, so last thing, I'm just going to head over to our navigation. At the moment, we'll be showing this
02:17
even for users who are registered and signed in, but we'll change this up a little bit later. OK, let's go over to our app, hit register, and yeah, cannot resolve a callable for this route.
02:27
So let's just have a look in our register controller and see if we're doing everything correctly. And yeah, the namespace wasn't created properly here. Let's add that under auth and go over to our web routes
02:38
and just update that in here. OK, great, let's give that a refresh, and there we go, we've got our register page. So the form that we're going to build
02:46
over on our register component or a register view will go through to the same route name, but the method will be different. So we'll register out two routes for this.
02:56
Let's go ahead and just fill out some of the fields that we're going to expect to see. So we'll wrap each of the fields in a div, and there's a few things that we want to include here.
03:06
So the first thing is going to be just an input for the name. So let's give this an ID of first name. You can break this up and change this around in the database if you just want a single name,
03:16
it's entirely up to you. And we'll not do anything else there. We'll just create our label for this, and this will be our first name.
03:25
And let's say this is for first name, so it hooks up. And we'll give this a name as well, because we're going to need that for our backend, and I think that's just about it.
03:33
So now that we've done this, let's just copy this down to another one. We'll change this over to email, and we'll change this over to email.
03:42
We'll set the type here as text, but we're going to look at validation a little bit later. I'm going to keep this as a type of text so it doesn't validate on the client side,
03:51
just so we can see our rules in action. And the last thing that we need is a password. So let's go ahead and change this over to password, and the text here over to password.
04:00
We will set this as a type of password. Okay, so last thing is just a button with a type of submit to send this through. Obviously, it is not going to look great at the moment,
04:11
but you can style it up if you want to. Okay, so we've got all of our fields. Now, if I hit register here, it's going to post through to register,
04:17
and we get a method not allowed exception. That's just because we haven't registered one. So what we're going to do is register a new route here with the same name, but we're going to change this to post,
04:27
and we're going to go and reference a store method inside of our register controller. So let's create out another method in here, and then by the end of this episode,
04:36
we'll be able to dump the data that has actually been sent through into this form. So we'll pass in store, and we know that we want a request in here,
04:45
so we can do exactly the same thing here to grab this out. Let's just var dump in here for now, kill the page, and just say store, just so we know that this is working.
04:56
Okay, so I can either refresh the browser to resend that or go back to the form, hit register, and there we go. We've successfully posted through to a route now. Okay, so how about grabbing the data?
05:07
We already looked at this earlier when we set up our request. So let's go and just use dump here, just so it's a little bit prettier, kill the page, and let's access from our request, get parsed body.
05:19
So this will give us the body of the request that we get through when we post through, and we should see all of the details that the user submits through.
05:27
So I'm going to go ahead and just fill this out really quickly, and let's take a look at what we get when we hit register. And there we go.
05:34
You can see that we've got our first name, email, and password in here within an array, and we can very easily take that data, send it through to Sentinel when we register a user.
54 episodes4 hrs 45 mins

Overview

Starting completely from scratch, build a modern PHP framework with all the features you’d expect.

Whether you’re new to PHP or not, this is a great exercise for learning what happens under the hood, arming you with knowledge you can apply anywhere you use PHP.

We’ll cover routing, controllers, views, the container, accessing the database, models, authentication, config, CSRF protection, exception handling, pagination, validation, flashing messages and much more.

Let’s dive in and build a PHP framework, step-by-step!

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!