در حال بارگذاری ...

New Laravel 5.7 Project0:00

Now that Laravel 5.7 is officially out, let's take a look around. laravel new, I'll create a project called Laravel 5.7. And just like that, we're all set to go. We now have a fresh Laravel 5.7 installation. First up, I'd like to show you email verification. So if we take a look at our User model, it's mostly the same, except for this one new import here, MustVerifyEmail. Let's take a look at that. That's a contract. We can see it includes about three methods.

Email Verification Setup0:32

That's a contract. We can see it includes about three methods. Now, if we were to implement that, behind the scenes, after a User registers, they'll receive a verification email. Let's take a look at this together. If I go into my .env file, I've already set up a database called Laravel 5.7, with a username of root and a password of nothing. So let's go ahead and migrate my database. And if I switch over to SQL Pro and give this a refresh, in my users table, you'll see that there's now an email

And if I switch over to SQL Pro and give this a refresh, in my users table, you'll see that there's now an email_verified_at timestamp included out of the box. So let's give this email verification a shot. Now, real quick, because we know an email is going to be fired off, for testing, I'm going to switch the mail driver to the log. Okay. Next, if I run php artisan, you may remember that a few versions ago, Laravel added this make:auth command. It quickly scaffolds a basic registration and login system.

Scaffold Auth and Test1:26

Laravel added this make:auth command. It quickly scaffolds a basic registration and login system. So I'm going to run that, and then view this in the browser. Okay. We now see links to register. So let's build an account for JohnDoe. And immediately, it blows up. RouteVerification.verify is not defined. But if we switch over to our User, give it a refresh, yes, we created a User, but at some point, the email verification blew up.

Enable Verification Routes1:50

But if we switch over to our User, give it a refresh, yes, we created a User, but at some point, the email verification blew up. Here's the issue. We're going to go to our routes/web.php file, and when we register the authentication routes, we're now going to specify that we do need email verification. All right. Let's give it one more shot. I'll do it again. And there we go.

Require Verified Emails2:13

I'll do it again. And there we go. We're now signed in. But still, if we give this a refresh, we'll see email_verified_at is null. You haven't yet verified your email. We don't know if this is real or not. So what can we do with this? Well, let me show you. If we go into our Http\Kernel.php, you'll see that there is a route middleware. If we scroll down, right here.

If we go into our HTTPKernel, you'll see that there is a route middleware. If we scroll down, right here. This is brand new in Laravel 5.7. EnsureEmailIsVerified. Take a look at that. If we have a User, and that User implements that interface, and the User has not verified their email, well, then, we're going to redirect them to a page to verify their email. That's all that middleware does. All right.

Okay. So now, if I were to come back and go to the homepage, instantly it redirects me here. It's saying, hey, I expected you to have a verified email address, but you haven't done it yet, so you don't have permission to visit this page. Either check your email or click here to send it again. Okay. Let's see if that email was sent. I'm going to go into storage/logs, and if I scroll down to the very bottom, here is the email.

How Verification Is Triggered4:18

Nice and clean. Now, if you're curious, well, what was going on there? Take a look at this just to make sure you understand what's happening. In my providers, EventServiceProvider, you'll see this in a new Laravel 5.7 app. Once a User has registered, trigger this event listener. Let's take a look at that. All right. Well, once again, we're checking. Did the User class implement that?

Well, once again, we're checking. Did the User class implement that? So let's say out of the box like this, you don't implement the interface. All right. Well, this listener will still fire, but because this doesn't pass, we don't do anything. But if we do implement that contract, Laravel will check for that, and then we will send the email verification notification. And all that's going to do is use Laravel's notifications. Create a new VerifyEmailNotification and fire that off to the user's inbox. And that's all there is to it.

Create a new VerifyEmail notification and fire that off to the user's inbox. And that's all there is to it. So this is a nice new addition in Laravel 5.7.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟