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
33. Logging in

Episodes

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

Transcript

00:00
Okay, let's now go through the flow again, but create a route to log the user in. So it's going to be exactly the same as our register flow. Feel free to go ahead and build it yourself if you want to try this out. Otherwise, let's get started.
00:13
So the first thing that we're going to need is a login controller. So I'm pretty much just going to copy and paste this register controller. So let's call this login controller and paste everything in. Obviously change over the name of this.
00:27
And everything is going to be pretty similar. The index is going to be similar here. We're just going to obviously render a login view. And in here, we'll fill this in in a second and we'll keep the redirect in here
00:37
because we are going to redirect the user over to the dashboard. Okay, so over to our web routes, let's go ahead and register two more of these. We'll look at route grouping a little bit later when things start to get messy. And let's say login, login and reference our login controller here and here.
00:54
And these two methods are the same. Okay, so last up, let's just create out the login.twig view. And again, we can pretty much steal everything from register because it's going to be very similar. Just changing over the title, the header, the action that we're going through to.
01:13
And we don't need our first name to log in. We just need our email and our password. And let's finally change that button label. Okay, so once again, let's add this to our navigation.
01:22
So let's, when we are not signed in, add in a login link here through to login. And if we head over, we should be able to click on this and enter our details. Okay, so to log a user in, we've actually already seen this over in our register controller, just here, this auth login.
01:41
Again, we're not going to add validation here. We're not going to check if the login failed. We will be doing that a little bit later. So let's go back over to our login controller.
01:50
And inside of the store, let's just take this. And instead of login, we're going to use authenticate. And you can also use authenticate and remember. So you could add a checkbox to this if you wanted to, to either pass this through,
02:03
as you can see, to the method. So if we just open up the authenticate method, you can see that we can pass a true or false value so you can implement a checkbox and send this through as well if you wanted to. Okay, so let's authenticate.
02:17
And of course, we're going to pass through all of the credentials that come through in the past body of the request. And then we're going to authenticate the user and send them over to the dashboard. So we'll tidy this up in just a second because we want to check if the login process failed.
02:32
But let's go and try and sign in with the details that I registered earlier. So my password is just password. Let's hit login. And there we go.
02:40
We are now signed in. Obviously, we have redirected to our dashboard. We see our name. So just to give you an idea of what we're going to cover later when we flash messages,
02:48
this, of course, can fail if I enter incorrect information. So if we head over to our cookies and get rid of our PHP session, let's go ahead and just submit this through. You can see that we're redirected because we're not creating any kind of if statements
03:04
in here to prevent that. And if I enter the incorrect credentials, we're not going to be signed in either. So what we're going to do with this is this will return false if it fails. So we can just wrap this entire thing in an if statement.
03:17
So if it does fail, what we can do is we can flash a message up here, which we're going to be covering a little bit later. And then we can redirect the user back to the login page. Let's go ahead and just make sure we check that this is falsy.
03:31
And let's go over and try and hit login. And you can see I'm just redirected straight back to the login page. If I enter my email address and I choose an incorrect password and hit login, again, that comes and redirects us back as well.
03:45
So that's pretty much what we're going to be doing later, both with the login and register process when we look at flashing messages. We just decide where we want to redirect the user to based on whether this was successful or failed.
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!