Laravel Auth,获取在通知中传递的默认URL

Laravel Auth,获取在通知中传递的默认URL,laravel,laravel-6,laravel-authentication,laravel-notification,laravel-6.2,Laravel,Laravel 6,Laravel Authentication,Laravel Notification,Laravel 6.2,我只是想在注册后更改默认身份验证的toMail()函数中通知中的->greeting()。我想保留验证URL等,但我被绊倒了。如果覆盖sendEmailVerificationnotification(),则整个邮件都会更改。如何获取最初应该发送的URL,或者如何编辑原始身份以仅编辑带有亲爱姓名的->问候语('Hello') 在用户模型中 public function sendEmailVerificationNotification() { $this->notify(new

我只是想在注册后更改默认身份验证的toMail()函数中通知中的->greeting()。我想保留验证URL等,但我被绊倒了。如果覆盖sendEmailVerificationnotification(),则整个邮件都会更改。如何获取最初应该发送的URL,或者如何编辑原始身份以仅编辑带有亲爱姓名的->问候语('Hello')

在用户模型中

public function sendEmailVerificationNotification()
{
    $this->notify(new CustomVerifyEmail());
}
/**
 * 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)
{
    //dd($notifiable);
    return (new MailMessage)
                ->greeting('Dear ' . $notifiable->name . ',')
                ->line('The introduction to the notification.')
                ->action('Notification Action', url())
                ->line('Thank you for using our application!');
}

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

public function sendEmailVerificationNotification()
{
    $this->notify(new CustomVerifyEmail());
}
/**
 * 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)
{
    //dd($notifiable);
    return (new MailMessage)
                ->greeting('Dear ' . $notifiable->name . ',')
                ->line('The introduction to the notification.')
                ->action('Notification Action', url())
                ->line('Thank you for using our application!');
}

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

有一种方法可以自定义由
VerifyEmail
发送的
MailMessage
,而无需重写任何方法或编写自己的通知类

illighte\Auth\Notifications\VerifyEmail
类实际上允许您分配自己的回调来处理通知的
toMail
端。此回调接收
$notifiable
$verificationUrl
。您可以尝试以下方法:

use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Notifications\Messages\MailMessage; 

VerifyEmail::$toMailCallback = function ($notifiable, $verificationUrl) {
    return (new MailMessage)
        ->greeting("Dear {$notifiable->name},")
        ->line('The introduction to the notification.')
        ->action('Notification Action', $verificationUrl)
        ->line('Thank you for using our application!');        
};
您可以将其放在服务提供商的
boot
方法中


如果您不想这样做,您可以扩展
VerifyEmail
通知来编写自己的
toMail
方法,但可以访问该功能以获取验证URL

use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Notifications\Messages\MailMessage; 

class CustomVerifyEmail extends VerifyEmail
{
    public function toMail($notifiable)
    {
        $verificationUrl = $this->verificationUrl($notifiable);

        return (new MailMessage)
            ...
    }
}

然后覆盖用户模型上的
sendEmailVerificationNotification
以发送自定义通知,就像您已经做的那样。

非常感谢@lagbox。我很快会给你答复的,没关系。这个答案属于这个线索。我一直在看别人的答案,但没有一个像你所说的那么完整。其他人缺乏进口(使用),所以我不能制造头部或尾部,但这一个真的很完美。再次感谢!:D