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]);

    }


   http://img1.mukewang.com/63952d340001da9812450625.jpg



料青山看我应如是
浏览 93回答 1
1回答

精慕HU

本教程对此进行了很好的解释。https://brabeum.com/2019/10/customise-the-laravel-user-verification-email/您应该首先在用户模型中覆盖在用户注册时发送通知的默认函数。/app/User.php/**&nbsp;* Override the default function that send a notification to verify&nbsp;* the email after a new user register.&nbsp;*&nbsp;*/&nbsp; public function sendEmailVerificationNotification()&nbsp; {&nbsp; &nbsp; &nbsp;$this->notify(new Notifications\UserVerificationEmail);&nbsp; }然后创建自定义通知:/app/Notifications/EmailVerification.php.<?phpnamespace 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{&nbsp; &nbsp; use Queueable;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Create a new notification instance.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @return void&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function __construct()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Get the notification's delivery channels.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param&nbsp; mixed&nbsp; $notifiable&nbsp; &nbsp; &nbsp;* @return array&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function via($notifiable)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return ['mail'];&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Get the mail representation of the notification.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param&nbsp; mixed&nbsp; $notifiable&nbsp; &nbsp; &nbsp;* @return \Illuminate\Notifications\Messages\MailMessage&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function toMail($notifiable)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return (new MailMessage)&nbsp; &nbsp; &nbsp; &nbsp; ->subject('Please verify your email address')&nbsp; &nbsp; &nbsp; &nbsp; ->markdown(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'emails.userverify',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'url' => $this->verificationUrl($notifiable),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'notifiable' => $notifiable,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Get the array representation of the notification.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param&nbsp; mixed&nbsp; $notifiable&nbsp; &nbsp; &nbsp;* @return array&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function toArray($notifiable)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; }&nbsp; &nbsp; /*&nbsp; &nbsp;* Build the verification URL&nbsp; &nbsp;*&nbsp; &nbsp;* @return URL&nbsp; &nbsp;*/&nbsp; &nbsp;protected function verificationUrl($notifiable)&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; return URL::temporarySignedRoute(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'verification.verify',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Carbon::now()->addMinutes(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Config::get('auth.verification.expire', 60)),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'id' => $notifiable->getKey(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'hash' => sha1($notifiable->getEmailForVerification()),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;);&nbsp;&nbsp; &nbsp;}}然后是可邮寄模板:/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@endcomponentIf 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 }}&nbsp;@endcomponent@endcomponent
打开App,查看更多内容
随时随地看视频慕课网APP