php - Laravel 自定义验证通知错误
问题描述
我正在尝试更新 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
我收到的错误
** ErrorException 未定义属性:App\Notifications\EmailVerification::$view **
解决方案
本教程对此进行了很好的解释。
https://brabeum.com/2019/10/customise-the-laravel-user-verification-email/
您应该首先在您的用户模型中覆盖在用户注册时发送通知的默认函数。
/app/User.php
/**
* Override the default function that send a notification to verify
* the email after a new user register.
*
*/
public function sendEmailVerificationNotification()
{
$this->notify(new Notifications\UserVerificationEmail);
}
然后创建自定义通知:
/app/Notifications/EmailVerification.php。
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\URL;
class UserVerificationEmail 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('Please verify your email address')
->markdown(
'emails.userverify',
[
'url' => $this->verificationUrl($notifiable),
'notifiable' => $notifiable,
]
);
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
/*
* Build the verification URL
*
* @return URL
*/
protected function verificationUrl($notifiable)
{
return URL::temporarySignedRoute(
'verification.verify',
Carbon::now()->addMinutes(
Config::get('auth.verification.expire', 60)),
[
'id' => $notifiable->getKey(),
'hash' => sha1($notifiable->getEmailForVerification()),
]
);
}
}
然后是可邮寄的模板:
/resources/views/emails/userverify.blade.php
@component('mail::message')
# Welcome {{ $notifiable->name }}
Before you can use this tutorial system you must verify your email address.
@component('mail::button', ['url' => $url])
Brabeum Verify Email Address Tutorial
@endcomponent
If you did not create an account, no further action is required.
Thanks,
{{ config('app.name') }} Team
@component('mail::subcopy')
If you’re having trouble clicking the "Verify Email Address" button, copy and paste the URL below into your web browser: {{ $url }}
@endcomponent
@endcomponent
推荐阅读
- c# - 字符串未被识别为有效的时间跨度。C# 和 SQL 服务器
- java - 无法在 AEM 新页面上拖放组件
- google-cloud-platform - 有没有一种快速的方法可以一次删除 VPC 及其所有依赖项?
- django - Django Api 只读字段
- scala - 在类型函数中使用匹配类型时证明类型相等的问题
- html - 为什么我不能只更改最后一个 h3 的字体大小
- huawei-mobile-services - 我已经成功集成了HUAWEI Location Kit。但现在得到错误权限被拒绝
- python - 在 15*8-2**x=x 中求解 x 的公式求解器未完成计算
- airflow - 使用 .net 控制台批处理应用程序进行气流编排
- javascript - Snowpack 无法从 node_modules 导入 JavaScript