自定义Laravel验证电子邮件

时间:2020-03-31 15:56:44

标签: laravel

请尝试自定义Laravel验证电子邮件

在我的用户模型中,我有这个

  public function sendEmailVerificationNotification()
    {
        $this->notify(new MyNotification);
    }

然后我跑了

php artisan make:notification MyNotification

里面有这个

<?php

namespace App\Notifications;
use Auth;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\URL;


class MyNotification extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

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

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {

        return (new MailMessage)
        ->subject('Verify Email Address')
        //I am trying output Hello User Name
        ->greeting('Hello!' {Auth::user()->name} )
        ->line('The introduction to the notification.')
        ->line('Please click the button below to verify your email address.')
        ->action(Lang::get('Verify Email Address'), $verificationUrl)
        ->line('If you did not create an account, no further action is required.')
        ->line('Thank you for using our application!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

打招呼('Hello!'{Auth :: user()-> name}

返回错误

非法字符串偏移量'John Doe'

action(Lang :: get('验证电子邮件地址'),$ verificationUrl)

返回的错误

未定义变量:verificationUrl

然后我添加了这个

   $verificationUrl = $this->verificationUrl($notifiable);

返回了新错误

调用未定义的方法App \ Notifications \ MyNotification :: verificationUrl()

我现在真的不知道该怎么办。

1 个答案:

答案 0 :(得分:1)

  1. 问候修复:
->greeting('Hello! ' . Auth::user()->name)
  1. 对于该VerificationUrl ,我认为您必须扩展 \ Illuminate \ Auth \ Notifications \ VerifyEmail ,而不是 Notification

查看此内容:https://github.com/laravel/framework/blob/6.x/src/Illuminate/Auth/Notifications/VerifyEmail.php