You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 5.5中如何替换sendResetLink为自定义sendChangeLink方法?

Custom Password Change Notification in Laravel (Without Modifying Vendor Files)

Hey there! Let's break this down step by step so you can safely implement your custom sendChangeLink method without touching the vendor files.

First: What's $this->broker()?

The $this->broker() call returns an instance of Laravel's Password Broker—it's the core service that handles password reset logic (like validating users, generating reset tokens, and sending notifications). By default, it uses the Illuminate\Auth\Passwords\PasswordBroker class, and you can get it via Password::broker() (which is what your controller's broker() method does).

Since we don't want to modify the vendor version of this class, we have two solid approaches to add your custom sendChangeLink method.


Approach 1: Create a Custom Password Broker

This is the cleanest way if you want to keep the broker pattern consistent with Laravel's built-in logic.

Step 1: Make a Custom Broker Class

Create a new class that extends Laravel's default Password Broker:

// app/Auth/Passwords/CustomPasswordBroker.php
<?php

namespace App\Auth\Passwords;

use Illuminate\Auth\Passwords\PasswordBroker as BasePasswordBroker;

class CustomPasswordBroker extends BasePasswordBroker
{
    /**
     * Send a custom password change link to the user
     *
     * @param array $credentials
     * @return string
     */
    public function sendChangeLink(array $credentials)
    {
        // 1. Find the user by email
        $user = $this->getUser($credentials);
        if (is_null($user)) {
            return static::INVALID_USER;
        }

        // 2. Generate a reset token (uses Laravel's built-in token logic)
        $token = $this->createToken($user);

        // 3. Send your custom notification (we'll create this next)
        $user->sendPasswordChangeNotification($token);

        return static::RESET_LINK_SENT;
    }
}

Step 2: Replace the Default Broker Binding

Tell Laravel to use your custom broker instead of the default one. Open App\Providers\AppServiceProvider.php and add this to the register() method:

use App\Auth\Passwords\CustomPasswordBroker;
use Illuminate\Auth\Passwords\PasswordBroker;

public function register()
{
    $this->app->bind(PasswordBroker::class, CustomPasswordBroker::class);
}

Step 3: Add the Custom Notification Method to Your User Model

In your App\Models\User.php file, add a method to send your custom notification:

use App\Notifications\ChangePasswordNotification;

public function sendPasswordChangeNotification($token)
{
    $this->notify(new ChangePasswordNotification($token));
}

Step 4: Create the Custom Notification

Generate a new notification class:

php artisan make:notification ChangePasswordNotification

Then update the generated file to match your custom email content:

// app/Notifications/ChangePasswordNotification.php
<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class ChangePasswordNotification extends Notification
{
    use Queueable;

    protected $token;

    public function __construct($token)
    {
        $this->token = $token;
    }

    public function via($notifiable)
    {
        return ['mail'];
    }

    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->subject('Your Custom Password Change Request')
            ->line('You requested to update your account password. Click the button below to set a new password.')
            ->action('Change Password', url(route('password.reset', [
                'token' => $this->token,
                'email' => $notifiable->email
            ])))
            ->line('If you didn\'t request this change, please ignore this email.');
    }
}

Step 5: Update Your Controller

Now you can safely use your custom method in ChangePasswordController.php:

public function changePassword(Request $request)
{
    $request['email'] = Auth::user()->email;
    $request['update'] = true;

    $response = $this->broker()->sendChangeLink(
        ['email' => Auth::user()->email]
    );

    return $response == Password::RESET_LINK_SENT
                ? $this->sendResetLinkResponse($response)
                : $this->sendResetLinkFailedResponse($request, $response);
}

Approach 2: Handle the Logic Directly in the Controller

If you don't want to create a custom broker, you can skip that and handle the token generation and notification directly in your controller:

public function changePassword(Request $request)
{
    $user = Auth::user();
    $request['email'] = $user->email;
    $request['update'] = true;

    // Generate a reset token using the default broker
    $token = app('auth.password.broker')->createToken($user);

    // Send your custom notification
    $user->notify(new \App\Notifications\ChangePasswordNotification($token));

    return back()->with('status', 'Password change link sent to your email!');
}

This approach is simpler if you only need this custom logic in one place.


Key Notes

  • Make sure your User model uses the Illuminate\Notifications\Notifiable trait (it does by default in Laravel).
  • Verify your password reset route (route('password.reset')) is correctly set up in routes/web.php.
  • Test the flow to ensure tokens are generated correctly and emails are sent with your custom content.

内容的提问来源于stack exchange,提问作者Howard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 10:08:07