【问题标题】:Customize Laravel Verification Email自定义 Laravel 验证邮件
【发布时间】:2020-07-12 06:00:23
【问题描述】:

请我尝试自定义 Laravel 验证电子邮件

在我的用户模型中,我有这个

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

然后我跑了

php artisan make:notification MyNotification

里面有这个

<?php

namespace App\Notifications;
use Auth;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\URL;


class MyNotification 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('Verify Email Address')
        //I am trying output Hello User Name
        ->greeting('Hello!' {Auth::user()->name} )
        ->line('The introduction to the notification.')
        ->line('Please click the button below to verify your email address.')
        ->action(Lang::get('Verify Email Address'), $verificationUrl)
        ->line('If you did not create an account, no further action is required.')
        ->line('Thank you for using our application!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

这个

greeting('Hello!' {Auth::user()->name}

返回错误

非法字符串偏移 'John Doe'

这个

action(Lang::get('验证电子邮件地址'), $verificationUrl)

返回错误

未定义变量:verificationUrl

然后我添加了这个,

   $verificationUrl = $this->verificationUrl($notifiable);

返回新错误

调用未定义的方法 App\Notifications\MyNotification::verificationUrl()

我现在真的不知道该怎么办。

【问题讨论】:

    标签: laravel


    【解决方案1】:
    1. 问候修复:
    ->greeting('Hello! ' . Auth::user()->name)
    
    1. 对于该 verifyUrl 我认为您必须扩展 \Illuminate\Auth\Notifications\VerifyEmail 而不是 Notification

    看到这个:https://github.com/laravel/framework/blob/6.x/src/Illuminate/Auth/Notifications/VerifyEmail.php

    【讨论】:

    • 谢谢,但是如何在 Hello 和用户名之间添加空格
    • @xtremeCODE 我添加了它,在!和 ' 有一个空格,这就是你要找的,只需复制我的代码
    • 确定没问题@xtremeCODE
    猜你喜欢
    • 1970-01-01
    • 2020-11-18
    • 2021-06-12
    • 2020-11-18
    • 2012-11-27
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 2015-05-10
    相关资源
    最近更新 更多