尝试在Laravel 5.5中设置更新用户密码的新通知

时间:2018-05-15 18:04:55

标签: php laravel notifications

因此,当用户更新密码时,我尝试发送自定义通知。 SendPasswordResetEmails.php文件位于供应商文件夹中,据我所知,我不应该直接修改那里的代码,因为它可以在更新中更改,什么不可以。所以我创建了一个名为ChangePasswordController.php的控制器,并复制粘贴了SendPasswordResetEmails.php中的一些方法,用于发送重置链接电子邮件。

在我的新控制器中,我有以下内容:

/**
 * Send a change link to the given user.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
 */
public function changePassword(Request $request) {
    $request['email'] = Auth::user()->email;
    $request['update'] = true; // user wishes to update/change their password

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

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

/**
 * Get the response for a successful password reset link.
 *
 * @param  string  $response
 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
 */
protected function sendResetLinkResponse($response)
{
    return back()->with('status', trans($response));
}

/**
 * Get the response for a failed password reset link.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  string  $response
 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
 */
protected function sendResetLinkFailedResponse(Request $request, $response)
{
    return back()->withErrors(
        ['email' => trans($response)]
    );
}

/**
 * Get the broker to be used during password reset.
 *
 * @return \Illuminate\Contracts\Auth\PasswordBroker
 */
public function broker()
{
    return Password::broker();
}

在changePassword方法中,注意sendResetLink中的行:

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

如何创建自定义方法sendChangeLink而不是sendResetLink?我不太明白$ this-> broker()部分是如何运作的,所以我不想搞砸它,但也许有人可以提供帮助。

0 个答案:

没有答案