【问题标题】:Setting a custom user verification link issue in laravel在 laravel 中设置自定义用户验证链接问题
【发布时间】:2020-08-14 20:57:49
【问题描述】:

我一直在尝试在 laravel 中向我的用户发送一封自定义验证电子邮件。

首先我运行这个

php artisan make:notification SendRegisterEmailNotifcation

这在我的App/Notifications 中创建了一个名为SendRegisterEmailNotifcation.php 的文件。

然后在我的用户控制器的存储方法中,我在用户插入完成后调用了该方法。

以下是我的商店功能,

public function store(Request $request)
    {
        request()->validate([
            'name' => ['required', 'alpha','min:2', 'max:255'],
            'last_name' => ['required', 'alpha','min:2', 'max:255'],
            'email' => ['required','email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:12', 'confirmed','regex:/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{12,}$/'],
            'mobile'=>['required', 'regex:/^\+[0-9]?()[0-9](\s|\S)(\d[0-9]{8})$/','numeric','min:9'],
            'username'=>['required', 'string', 'min:4', 'max:10', 'unique:users'],   
            'roles'=>['required'],
            'user_roles'=>['required'],
        ]);

        //Customer::create($request->all());

        $input = $request->all();
        $input['password'] = Hash::make($input['password']);

        $user = User::create($input);
        $user->assignRole($request->input('roles'));

        //event(new Registered($user));
        $user->notify(new SendRegisterMailNotification());

        return redirect()->route('customers.index')
                        ->with('success','Customer created successfully. Verification email has been sent to user email.  ');
    }

这是我的SendRegisterMailNotification.php

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class SendRegisterMailNotification 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)
                    ->line('The introduction to the notification.')
                    ->action('Click Here to Activate', url('/'))
                    ->line('Thank you for using our application!');
    }

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

现在这个过程运行良好,新创建的用户正在接收他们的电子邮件。

但问题是

通常在 laravel 中激活链接具有一定的格式,一旦用户点击按钮用户的帐户被激活并将验证的日期时间存储在用户表中,链接也会在 60 分钟内过期..

示例验证链接,

http://test.site/email/verify/22/3b7c357f630a62cb2bac0e18a47610c245962182?expires=1588247915&signature=7e6869deb1b6b700dcd2a49b2ec66ae32fb0b6dc99aa0405095e9844962bb53c

但就我而言,我很难正确设置激活链接和流程,我该如何使用上述自定义电子邮件来做到这一点?

【问题讨论】:

  • 你能发布应该处理代码验证的代码吗?

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


【解决方案1】:

您可以在SendRegisterMailNotification 通知类中使用Illuminate\Auth\Notifications\VerifyEmail 库中VerifyEmail 类的verificationUrl() 函数获取验证电子邮件的链接。

来了,

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Auth\Notifications\VerifyEmail;

class SendRegisterMailNotification extends VerifyEmail implements ShouldQueue
{
    use Queueable;

/**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        $actionUrl  = $this->verificationUrl($notifiable);  //call the verificationUrl() from base class
        $actionText  = 'Click here to verify your email';
        
        /*here, i am using view blades instead of markdown for email template*/
        return (new MailMessage)->subject('Verify your account')->view(
            'emails.user-verify',
            [
                'actionText' => $actionText,
                'actionUrl' => $actionUrl,
            ]);
    }

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

emails.user-verify 视图如下所示

        <p>
            Please click the button below to verify your email address.
        </p>

        
        <a href="{{ $actionUrl }}" class="button">{{$actionText}}</a>
        
        <p>If you did not create an account, no further action is required.</p>

        <p>
            Best regards, <br><br>
            <strong>{{config('app.signature')}}</strong><br/>
            {{ config('app.signature-title')}}
        </p>

或者如果你更喜欢markdown,你可以使用

public function toMail($notifiable)
{
    $url= $this->verificationUrl($notifiable);


      return (new MailMessage)
                    ->line('The introduction to the notification.')
                    ->action('Click Here to Activate', $url)
                    ->line('Thank you for using our application!');

}

【讨论】:

    猜你喜欢
    • 2020-11-18
    • 2013-01-04
    • 2020-07-21
    • 1970-01-01
    • 2020-08-14
    • 1970-01-01
    • 2013-06-21
    • 2020-08-20
    • 2021-01-14
    相关资源
    最近更新 更多