覆盖Laravel 5.3中的默认密码重置过程

I am currently writing an application that is using Laravel 5.3 on the backend, and I'm looking for a way to overwrite the default password reset behaviour.

The class that I need to change is "ResetPassword" located here: /Illuminate/Auth/Notifications/ResetPassword.php

Reason for the change is, that the reset url generated in this file is not correct for my front-end - as it uses url(), which puts the API url rather then the front-end url in the reset email.

You can override CanResetPassword's sendPasswordResetNotification() method in your User.php

use Illuminate\Notifications\Notifiable;
use App\Notifications\CustomResetPasswordNotification;

public function sendPasswordResetNotification($token)
{
    $this->notify(new CustomResetPasswordNotification($token));
}

and create CustomResetPasswordNotification.php according your requirements.

Check Password Reset Emails section here for more details

I found a quick and easy way to overwrite the password reset process by overwriting the User class located here:

/Illuminate/Foundation/Auth/User.php

Basically, I created my own version like this:

<?php

namespace App\Traits\Auth;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use App\Traits\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements
    AuthenticatableContract,
    AuthorizableContract,
    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword;
}

I saved it to /App/Traits/Auth and now use it in my User model.

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use App\Traits\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

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

Once finished, you can create your own version of the "CanResetPassword" trait and replace the Notification class and make necesarry adjustments.

Here is an example replacement for the "CanResetPassword" trait:

namespace App\Traits\Auth\Passwords;

use App\Notifications\CustomResetPassword as ResetPasswordNotification;

trait CanResetPassword
{
    /**
     * Get the e-mail address where password reset links are sent.
     *
     * @return string
     */
    public function getEmailForPasswordReset()
    {
        return $this->email;
    }

    /**
     * Send the password reset notification.
     *
     * @param  string  $token
     * @return void
     */
    public function sendPasswordResetNotification($token)
    {
        $this->notify(new ResetPasswordNotification($token));
    }
}