Laravel Auth, Getting the default URL that is passed in Notification

放肆的年华 提交于 2019-12-11 17:38:39

问题


I just wanted to change the ->greeting() in notification in my toMail() function of the default auth after registering. I want to retain the verify URL and etc. But I am stucked. If I override the sendEmailVerificationnotification() the whole mail is changed. How to get the URL that was supposed to be sent originally or how to edit the original auth to edit only the ->greeting('Hello') with Dear Name, ?

In User Model

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

In 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 [
        //
    ];
}

回答1:


There is a way to be able to customize the MailMessage that is sent by VerifyEmail without overriding any methods or writing your own Notification class.

The Illuminate\Auth\Notifications\VerifyEmail class will actually let you assign your own callback for handling the toMail side of the Notification. This callback receives the $notifiable and the $verificationUrl. You could try something like this:

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!');        
};

You could put that in a Service Provider's boot method.


If you didn't want to go that way you could extend the VerifyEmail Notification to write your own toMail method, but have access to the functionality to get the verification 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)
            ...
    }
}

Then override the sendEmailVerificationNotification on the User Model to send the custom notification, like you have done already.



来源:https://stackoverflow.com/questions/58897683/laravel-auth-getting-the-default-url-that-is-passed-in-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!