如何在Laravel 5.2中手动发送密码重置请求?

2022-08-30 22:36:37

我想从控制器中手动向特定用户(不是当前登录的用户)发送密码重置请求。我在Laravel代码中做了一些挖掘,似乎我应该打电话进来,但我似乎不知道如何访问正确的实例来调用它。postEmail(Request $request)ResetsPasswordsPasswordController


答案 1

为什么不为您的控制器做这样的事情:

<?php

namespace Illuminate\Foundation\Auth;

use Illuminate\Http\Request;
use Illuminate\Mail\Message;
use Illuminate\Support\Facades\Password;

class YourController extends Controller
{
    public function sendEmail()
    {
        $credentials = ['email' => $email_address];
        $response = Password::sendResetLink($credentials, function (Message $message) {
            $message->subject($this->getEmailSubject());
        });

        switch ($response) {
            case Password::RESET_LINK_SENT:
                return redirect()->back()->with('status', trans($response));
            case Password::INVALID_USER:
                return redirect()->back()->withErrors(['email' => trans($response)]);
        }
    }
}

您并没有真正解释要如何发送此内容的上下文,因此请进行相应的调整。


答案 2

感谢Mariusz Kurman,我只在他的答案中添加了令牌。这工作得很好:

$user = User::where('email', request()->input('email'))->first();
$token = Password::getRepository()->create($user);
$user->sendPasswordResetNotification($token);

推荐