Laravel自定义用户验证电子邮件问题

时间:2020-07-29 09:58:46

标签: php laravel customization laravel-6 email-verification

在laravel应用程序中,我正在尝试创建自定义的用户验证电子邮件模板。

首先,我在App内创建了一个名为Notifications的文件夹,在Notifications文件夹内有一个名为CustomVerifyEmailNotification.php的文件

app/Notifications/CustomVerifyEmailNotification.php

以下是我在CustomVerifyEmailNotification.php中的代码

<?php

namespace Illuminate\Auth\Notifications;

use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\URL;

class CustomVerifyEmailNotification extends Notification
{
    /**
     * The callback that should be used to build the mail message.
     *
     * @var \Closure|null
     */
    public static $toMailCallback;

    /**
     * Get the notification's channels.
     *
     * @param  mixed  $notifiable
     * @return array|string
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Build the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        $verificationUrl = $this->verificationUrl($notifiable);

        if (static::$toMailCallback) {
            return call_user_func(static::$toMailCallback, $notifiable, $verificationUrl);
        }

        return (new MailMessage)
            ->subject(Lang::get(''.('sentence.Verify Email Address').''))
            ->line(Lang::get(''.('sentence.If you did not create an account, no further action is required.').''));
    }

    /**
     * Get the verification URL for the given notifiable.
     *
     * @param  mixed  $notifiable
     * @return string
     */
    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()),
            ]
        );
    }

    /**
     * Set a callback that should be used when building the notification mail message.
     *
     * @param  \Closure  $callback
     * @return void
     */
    public static function toMailUsing($callback)
    {
        static::$toMailCallback = $callback;
    }
}

然后我根据以下代码更改了User.php

<?php

namespace App;

use App/Notifications/CustomVerifyEmailNotification;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Cashier\Billable;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable,Billable;
    use HasRoles;

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

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */

    protected $fillable = [
        'name','last_name', 'email', 'password','username','mobile','propic','user_roles','user_source',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

但是

use App/Notifications/CustomVerifyEmailNotification;

显示一个错误,提示syntax error, unexpected '/', expecing ';' or ','

因此,我在这里也遇到了错误

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

错误是undefined type 'App\CustomVerifyEmailNotification'

如何解决此问题并创建此自定义验证电子邮件

我正在使用laravel 6

1 个答案:

答案 0 :(得分:1)

名称空间应为

namespace App\Notifications

不是

namespace Illuminate\Auth\Notifications;

user.php中而不是

use App/Notifications/CustomVerifyEmailNotification;

您应该使用

use App\Notifications\CustomVerifyEmailNotification;