Laravel Auth,获取在通知中传递的默认URL [英] Laravel Auth, Getting the default URL that is passed in Notification
问题描述
我只想在注册后更改默认身份验证的toMail()函数的通知中的-> greeting().我想保留验证URL等.但是我被卡住了.如果我覆盖sendEmailVerificationnotification(),则整个邮件都会更改.如何获取原本应该发送的URL或如何编辑原始身份验证以仅编辑-> greeting('Hello')的亲爱的姓名"?
在用户模型中
public function sendEmailVerificationNotification()
{
$this->notify(new CustomVerifyEmail());
}
在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
,而无需覆盖任何方法或编写自己的Notification类.>
Illuminate\Auth\Notifications\VerifyEmail
类实际上将允许您分配自己的回调来处理Notification的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
以发送自定义通知,就像您已经做过的那样.
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 [
//
];
}
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.
这篇关于Laravel Auth,获取在通知中传递的默认URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!