亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Laravel 驗證碼認證學習記錄小結(jié)

 更新時間:2019年12月20日 11:31:44   作者:gongmeng  
這篇文章主要介紹了Laravel 驗證碼認證學習記錄小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

注:此處所用的注冊等系列功能,均作用于 laravel 自帶的用戶認證機制

注冊驗證碼

1. composer 安裝驗證碼

composer require "mews/captcha:~3.0"

2. 運行以下命令生成配置文件 config/captcha.php

php artisan vendor:publish --provider='Mews.aptcha.aptchaServiceProvider' 

3. 前端展示驗證碼

<img class="thumbnail captcha mt-3 mb-2" src="{{ captcha_src('flat') }}" onclick="this.src='/captcha/flat?'+Math.random()" title="點擊圖片重新獲取驗證碼">

4. 后端驗證(該擴展包是為 Laravel 定制的擴展包,完全兼容 laravel 注冊功能,驗證非常方便)只需要在 app/Http/Controllers/Auth/RegisterController.php 中的驗證規(guī)則中加入如下代碼:

 'captcha' => ['required', 'captcha'],

郵箱認證

laravel 自帶郵箱認證

laravel 自帶的郵箱認證文件位于 vendor/laravel/framework/src/Illuminate/Auth/MustVerifyEmail.php ,因為此文件采用 trait 聲明,因此我們快速的將內(nèi)容通過 use 集成到用戶的模型中,并進行調(diào)用,如下:

  namespace App\Models;
  use Illuminate\Foundation\Auth\User as Authenticatable;
  use Illuminate\Notifications\Notifiable;
  use Illuminate\Auth\MustVerifyEmail as MustVerifyEmailTrait;
  class User extends Authenticatable {
    use Notifiable, MustVerifyEmailTrait;

為了代碼的規(guī)范,我們可以接入 larave 自帶的郵箱驗證接口 Illuminate\Contracts\Auth\MustVerifyEmail,規(guī)定模型必須擁有相關(guān)的方法,具體方法如下(方法在 MustVerifyEmailTrait 中已經(jīng)定義好,直接調(diào)用既可):

  • hasVerifiedEmail() 檢測用戶 Email 是否已認證;
  • markEmailAsVerified() 將用戶標示為已認證;
  • sendEmailVerificationNotification() 發(fā)送 Email 認證的消息通知,觸發(fā)郵件的發(fā)送;
  • getEmailForVerification() 獲取發(fā)送郵件地址,提供這個接口允許你自定義郵箱字段。

發(fā)送郵件

我們使用了 Laravel 自帶的 RegisterController ,控制器通過加載 Illuminate.oundation.uth.egistersUsers trait 來引入框架的注冊功能,此時我們打開此 trait 來翻閱源碼并定位到 register(Request $request) 方法:

public function register(Request $request) { 
// 檢驗用戶提交的數(shù)據(jù)是否有誤 
$this->validator($request->all())->validate(); 
// 創(chuàng)建用戶同時觸發(fā)用戶注冊成功的事件,并將用戶傳參 
event(new Registered($user = $this->create($request->all()))); 
// 登錄用戶 
$this->guard()->login($user); 
// 調(diào)用鉤子方法`registered()`
return $this->registered($request, $user) ?: redirect($this->redirectPath()); 
}

此方法處理了用戶提交表單后的邏輯,我們把重點放在 event(new Registered($user = $this->create($request->all())));,這里使用了 Laravel 的事件系統(tǒng),觸發(fā)了 Registered 事件。

打開 app/Providers/EventServiceProvider.php 文件,此文件的 $listen 屬性里我們可以看到注冊了 Registered 事件的監(jiān)聽器:

protected $listen = [ 
  Registered::class => [ SendEmailVerificationNotification::class, ], 
];

打開 SendEmailVerificationNotification 類,閱讀其源碼:vendor/laravel/framework/src/Illuminate/Auth/Listeners/SendEmailVerificationNotification.php

<?php 
namespace Illuminate\Auth\Listeners;

use Illuminate\Auth\Events\Registered;
use Illuminate\Contracts\Auth\MustVerifyEmail;

class SendEmailVerificationNotification
{
  /**
   * 處理事件
   *
   * @param \Illuminate\Auth\Events\Registered $event
   * @return void
   */
  public function handle(Registered $event)
  {
    // 如果 user 是繼承于 MustVerifyEmail 并且還未激活的話
    if ($event->user instanceof MustVerifyEmail && ! $event->user->hasVerifiedEmail()) {
      // 發(fā)送郵件認證消息通知(認證郵件)
      $event->user->sendEmailVerificationNotification();
    }
  }
}

可以看出 Laravel 默認已經(jīng)為我們設置了郵件發(fā)送的邏輯,接下來我們來測試一下。

測試郵件

測試之前,我們先設置下郵件發(fā)送到 log 中,以便后面的測試:

修改環(huán)境設置文件.env

MAIL_DRIVER=log

郵件一般發(fā)在 storage/logs 目錄下的日志文件中

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論