【发布时间】:2020-04-05 12:01:32
【问题描述】:
我正在尝试更新 Laravel 中的验证电子邮件通知。我尝试在 AppServiceProvider 中生成验证链接,然后将链接传递给通知类,但后来它给了我一个错误,即“未定义的属性 ::$view”。
应用服务提供者
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
VerifyEmail::toMailUsing(function ($notifiable) {
$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
Carbon::now()->addMinutes(config('auth.verification.expire', 60)),
[
'id' => $notifiable->getKey(),
'hash' => sha1($notifiable->getEmailForVerification()),
]
);
return new EmailVerification($verificationUrl);
});
}
验证电子邮件
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class EmailVerification extends Notification implements ShouldQueue
{
use Queueable;
public $verificationUrl;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($verificationUrl)
{
$this->verificationUrl = $verificationUrl;
}
/**
* 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)
{
$verificationUrl = $this->verificationUrl;
return (new MailMessage)
->subject('Please verify your email')
->markdown('emails.verification', ['url' => $verificationUrl]);
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
与通知相关的降价视图。
@component('mail::message')
# Email verification - {{ config('app.name') }}
Your registration on our application <b> {{ config('app.name') }} </b> was successfull. Kindly click the button below to verify your email address.
@component('mail::button', ['url' => $url])
Verify Email
@endcomponent
Thanks,<br>
{{ config('app.name') }}
@endcomponent
我收到的错误
** 错误异常 未定义的属性:App\Notifications\EmailVerification::$view **
【问题讨论】:
-
你使用的是哪个 laravel 版本?
-
Laravel 7.2 并尝试自定义验证邮件通知
-
我检查了我的环境。它正常工作。我希望您将通知类生成为“php artisan make:notification EmailVerification --markdown=verification”,然后使用“\Illuminate\Auth\Notifications\VerifyEmail::toMailUsing()”函数。
-
仍然是同样的问题,我将 URL 作为通知类中的公共属性传递,一切都很好但没有通过,但是,如果我直接在 AppServiceProvider 中编写通知,那么它可以正常工作但是我想分开做。使用错误屏幕截图更新问题。
-
您如何在控制器中单独使用它?根据您的问题,您可以覆盖应用服务提供商中的验证邮件功能。所以下次每当用户注册时,您的自定义电子邮件将发送给用户以验证他/她的电子邮件。但是您在控制器中的哪个位置单独使用它?