# 邮箱验证

Laravel 5.7引入了一个新特性，验证他们注册时的邮箱地址。

要启用邮箱验证，更新你的App\User类然后是其实现Illuminate\Contracts\Auth\MustVerifyEmail契约，如示例9-9所示

{% code title="Example 9-9. Adding the MustVerifyEmail trait to an Authenticatable model" %}

```php
class User extends Authenticatable implements MustVerifyEmail {
    use Notifiable; 
    // ...
}
```

{% endcode %}

users表必须包含一个非空的时间戳字段名为email\_verified\_at，在Laravel 5.7版本及之后版本迁移已经默认提供了该字段。

最后，你需要在控制器启用邮箱验证路由，最简单的方式是在路由文件使用Auth::routes()，然后将verify参数设置为true。

```php
Auth::routes(['verify' => true]);
```

现在，您可以保护任何未经验证邮件地址的用户访问您的路由。

```php
Route::get('posts/create', function () { 
    // Only verified users may enter...
})->middleware('verified');
```

你可以自定义路由，在VerificationController验证后用户的重定向。

```php
protected $redirectTo = '/profile';
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://liseen315.gitbook.io/laravel/yong-hu-ren-zheng-yu-shou-quan/you-xiang-yan-zheng.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
