Search code examples
laravelemail-verificationlaravel-5.7

Changing the “VerifyEmail” class for custom parameter in verification link in mail for method verificationUrl() in verification email in laravel 5.7


verification email that come with laravel 5.7. How and where i need to change it? I had searched all over online, but because it is brand new feature in 5.7,So i could not find an answer. Can you help me please? Thanks in advance.

basically that class is under Illuminate\Auth\Notifications

i want to override one of the method:

 class VerifyEmail extends Notification
        {
          // i wish i could override this method
           protected function verificationUrl($notifiable)
            {
             return URL::temporarySignedRoute('verification.verify', Carbon::now()->addMinutes(60), ['id' => $notifiable->getKey()]);
            } 
        }

Solution

  • Because your User Model uses Illuminate\Auth\MustVerifyEmail you can override the method sendEmailVerificationNotification which is the method that notifies the created user by calling the method notify and passes, as a parameter, a new instance of the Notifications\MustVerifyEmail class.

    You can create a custom Notification which will be passed as parameter to $this->notify() within the sendEmailVerificationNotification method in your User Model:

    public function sendEmailVerificationNotification()
    {
        $this->notify(new App\Notifications\CustomVerifyEmail);
    }
    

    And in your CustomVerifyEmail Notification you can define the route through which the verification will be handled and all parameters that it will take.

    When a new user signs up an Illuminate\Auth\Events\Registered Event is emitted in the App\Http\Controllers\Auth\RegisterController and that event has a listener Illuminate\Auth\Listeners\SendEmailVerificationNotification which is registered in the App\Providers\EventServiceProvider:

    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ]
    ];
    

    This listener checks if the $user, which is passed as a parameter to new Registered($user = $this->create($request->all())) in the Laravel default authentication App\Http\Controllers\Auth\RegisterController, is an instance of Illuminate\Contracts\Auth\MustVerifyEmail which is the trait that Laravel suggest to use in the App\User Model when you want to provide default email verification and check also that $user is not already verified. If all that passes it will call the sendEmailVerificationNotification method on that user:

    if ($event->user instanceof MustVerifyEmail && !$event->user->hasVerifiedEmail()) {
            $event->user->sendEmailVerificationNotification();
    }