邮箱验证 是如何工作的?

在查看邮箱验证相关源码的时候,打开 vendor/laravel/framework/src/Illuminate/Auth/Listeners/SendEmailVerificationNotification.php

public function handle(Registered $event)
{
    if ($event->user instanceof MustVerifyEmail && ! $event->user->hasVerifiedEmail()) {
        $event->user->sendEmailVerificationNotification();
    }
}

满足$event->user instanceof MustVerifyEmail && ! $event->user->hasVerifiedEmail()之后,执行 $event->user->sendEmailVerificationNotification()

public function sendEmailVerificationNotification()
{
    $this->notify(new Notifications\VerifyEmail);
}

我想请教下,$this->notify(new Notifications\VerifyEmail) 具体是如何工作的?

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
最佳答案

调用IlluminateNotificationSender 的 send 方法

/**
 * Send the given notification to the given notifiable via a channel.
 *
 * @param  mixed  $notifiable
 * @param  string  $id
 * @param  mixed  $notification
 * @param  string  $channel
 * @return void
 */
protected function sendToNotifiable($notifiable, $id, $notification, $channel)
{
    if (! $notification->id) {
        $notification->id = $id;
    }

    if (! $this->shouldSendNotification($notifiable, $notification, $channel)) {
        return;
    }

    $response = $this->manager->driver($channel)->send($notifiable, $notification);

    $this->events->dispatch(
        new Events\NotificationSent($notifiable, $notification, $channel, $response)
    );
}

其中$response = $this->manager->driver($channel)->send($notifiable, $notification); 创建了MailChannel,再往后看就基本理通了~

4年前 评论
讨论数量: 4

这就是一个普通的 消息通知 啊,然后在通知中使用的是邮件通知,你看看源码,写得很清楚的。

namespace Illuminate\Auth\Notifications;

use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Config;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;

class VerifyEmail extends Notification
{
    /**
     * The callback that should be used to build the mail message.
     *
     * @var \Closure|null
     */
    public static $toMailCallback;

    /**
     * Get the notification's channels.
     *
     * @param  mixed  $notifiable
     * @return array|string
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Build the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        $verificationUrl = $this->verificationUrl($notifiable);

        if (static::$toMailCallback) {
            return call_user_func(static::$toMailCallback, $notifiable, $verificationUrl);
        }

        return (new MailMessage)
            ->subject(Lang::getFromJson('Verify Email Address'))
            ->line(Lang::getFromJson('Please click the button below to verify your email address.'))
            ->action(Lang::getFromJson('Verify Email Address'), $verificationUrl)
            ->line(Lang::getFromJson('If you did not create an account, no further action is required.'));
    }

    /**
     * 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()]
        );
    }

    /**
     * Set a callback that should be used when building the notification mail message.
     *
     * @param  \Closure  $callback
     * @return void
     */
    public static function toMailUsing($callback)
    {
        static::$toMailCallback = $callback;
    }
}
4年前 评论

@lianglunzhong .我好奇的就是怎么走到 toMail 的...源码看了好久才看懂了 :ok_hand:

4年前 评论

调用IlluminateNotificationSender 的 send 方法

/**
 * Send the given notification to the given notifiable via a channel.
 *
 * @param  mixed  $notifiable
 * @param  string  $id
 * @param  mixed  $notification
 * @param  string  $channel
 * @return void
 */
protected function sendToNotifiable($notifiable, $id, $notification, $channel)
{
    if (! $notification->id) {
        $notification->id = $id;
    }

    if (! $this->shouldSendNotification($notifiable, $notification, $channel)) {
        return;
    }

    $response = $this->manager->driver($channel)->send($notifiable, $notification);

    $this->events->dispatch(
        new Events\NotificationSent($notifiable, $notification, $channel, $response)
    );
}

其中$response = $this->manager->driver($channel)->send($notifiable, $notification); 创建了MailChannel,再往后看就基本理通了~

4年前 评论

VerificationController里面处理验证邮箱的逻辑呢 怎么都没有涉及到呢 用户点击验证链接 然后处理user表里面verify_at字段的逻辑呢 怎么感觉虎头蛇尾呢

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!