Laravel 如何產生 Email 驗證網址
2020年6月4日
PHP Laravel由於新專案決定使用 Laravel 開發,在實作 Email 驗證時發現與 Devise 不同的是,它沒有在資料表內建立 confirmation_token
欄位,於是便讓我想了解一下它產生驗證網址的方式。
在追蹤原始碼後發現驗證網址是透過 verificationUrl
函數所產生的。
/**
* Get the verification URL for the given notifiable.
*
* @param mixed $notifiable
* @return string
*/
protected function verificationUrl($notifiable)
{
return URL::temporarySignedRoute(
'verification.verify',
Carbon::now()->addMinutes(Config::get('auth.verification.expire', 60)),
[
'id' => $notifiable->getKey(),
'hash' => sha1($notifiable->getEmailForVerification()),
]
);
}
由以上程式碼可看到它透過了 UrlGenerator
提供的 temporarySignedRoute
函數來產生暫時性的驗證網址。