在Laravel中捕获重置密码链接

时间:2018-02-23 03:35:13

标签: php laravel

关于我的问题:

Creating Password Reset Function without using Laravel make:auth

邮件程序在我的电子邮件中发送了重置电子邮件网址。但是,如果我点击链接,则找不到该对象,因为我的routes.php

中不存在该网址

我的问题:

如何抓住重置密码链接?

我的控制器:

   public function recover(Request $request)
{
    $user = Admin::where('email', $request->email)->first();
    if (!$user) {
        $error_message = "Your email address was not found.";
        return redirect()->back()->with(['errors' => $error_message]);
    }
    try {
        Password::sendResetLink($request->only('email'), function (Message $message) {
            $message->subject('Your Password Reset Link');
        });
    } catch (\Exception $e) {
        $error_message = $e->getMessage();
        return redirect()->back()->with(['errors' => $error_message]);
    }
    return redirect()->back()->with(['success' => 'A reset email has been sent! Please check your email.']);
}

我的路线:

Route::post('/forgot', [
'uses' => 'RegisterController@recover',
'as' => 'password.reset'
]);
Route::get('password/reset/{token?}', 'Auth\PasswordController@showResetForm');

重设密码中提供的电子邮件链接:

http://localhost/forgot?c2108023762b4f86029d5758207cb4156fd58052ad9d6b13b729ce84092937de

1 个答案:

答案 0 :(得分:1)

您可以通过 Request::query() 功能捕捉到这一点。 请参阅以下示例代码:

// http://localhost/forgot?c2108023762b4f86029d5758207cb4156fd58052ad9d6b13b729ce84092937de

Route::get('forgot', function (Illuminate\Http\Request $request) {
    $query = $request->query();
    $token = (array_keys($query))[0];
    echo $token; // c2108023762b4f86029d5758207cb4156fd58052ad9d6b13b729ce84092937de
});