如何在 Laravel 5.2 中手动发送密码重置请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38905761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I manually send a password reset request in Laravel 5.2?
提问by Daniel Centore
I would like to manually send a password reset request to a specific user (not the one currently logged in) from within a controller. I did some digging around in the Laravel code and it seems like I should be calling postEmail(Request $request)
in ResetsPasswords
, but I can't seem to figure out how to get access to the right PasswordController
instance to call it.
我想从控制器内手动向特定用户(不是当前登录的用户)发送密码重置请求。我做了一些周围挖在Laravel代码,它好像我应该打电话postEmail(Request $request)
的ResetsPasswords
,但我似乎无法弄清楚如何获得访问权PasswordController
的实例来调用它。
回答by Jared Eitnier
Why not just something like this for your controller:
为什么不为您的控制器做这样的事情:
<?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)]);
}
}
}
You don't really explain the context of how you want to send this, so adjust accordingly.
你并没有真正解释你想如何发送它的上下文,所以相应地调整。
回答by kjdion84
Complete control for Laravel 5.5:
Laravel 5.5 的完全控制:
$user = User::where('email', request()->input('email'))->first();
$token = Password::getRepository()->create($user);
Mail::send(['text' => 'emails.password'], ['token' => $token], function (Message $message) use ($user) {
$message->subject(config('app.name') . ' Password Reset Link');
$message->to($user->email);
});
回答by Shayan de
Thanks to Mariusz Kurman, I only added token to his answer. this works just fine:
感谢 Mariusz Kurman,我只在他的回答中添加了标记。这工作得很好:
$user = User::where('email', request()->input('email'))->first();
$token = Password::getRepository()->create($user);
$user->sendPasswordResetNotification($token);
回答by Mariusz Kurman
The easiest way:
最简单的方法:
$token = Str::random(60);
$user = User::where('email', request()->input('email'))->first();
$user->sendPasswordResetNotification($token);
And if you want to edit your e-mail manually:
如果您想手动编辑您的电子邮件:
php artisan vendor:publish
select "11" gives you:
选择“11”给你:
/resources/views/vendor/notifications/email.blade.php