当用户登录时,我已为该用户添加了创建新用户的功能(指定电子邮件地址,无需提供密码)。
完成后,我希望新用户收到一封电子邮件(如带有链接的重置密码电子邮件),将用户发送到设置密码页面(类似于重置密码视图) )。
我想出了如何在商店功能(UserController)中向新用户发送常规电子邮件:
public function store(UsersRequest $request)
{
$user = User::create(Request::all());
Mail::send('users.welcomemail', [], function ($message) {
$message->from('email@example.com', 'Email');
$message->to('email@example.com', 'Email')->subject('Welcome!');
});
return redirect('business/');
}
我为设置密码创建了一个新视图(从views / auth / reset.blade.php复制)。
我只是不确定我应该写什么(对于设置密码)到我的控制器,所以它的行为类似于重置密码功能。任何想法都会有所帮助。
如果可能,我想使用laravel中已有的内容..默认用户表和控制器,以及password_resets表。
更新 - 解决方案
我设法让它发挥作用。
UsersController
public function store(UsersRequest $request)
{
$user = User::create(Request::all());
$contactfirstname = $user->first_name;
$contactemail = $user->email;
$token = hash_hmac('sha256', str_random(40), config('app.key'));
DB::table('password_resets')->insert(['email' => $user->email, 'token' => $token, 'created_at' => \Carbon\Carbon::now()->toDateTimeString()]);
Mail::send('users.welcomemail', ['user' => $user, 'token' => $token], function ($message) use ($contactfirstname, $contactemail)
{
$message->from('name@email.com', 'My name');
$message->to($contactemail, $contactfirstname)->subject('Welcome!');
});
return redirect('business/');
}
users.welcomemail视图
<h1>Hi! {{ $user->first_name }}</h1>
<p>We'd like to personally welcome you. Thank you for registering!</p>
<p>Please click the link below to set your account password and get access to your account :</p>
<p><a href="{{ URL::to('auth/passwordset/' . $token) }}">{{ URL::to('auth/passwordset/' . $token) }}</a></p>
路线
('/auth/passwordset/{token}', 'PasswordSetupController@passwordset');
passwordSetupController
class PasswordsetController extends Controller {
/*
|--------------------------------------------------------------------------
| Passwordset Controller
|--------------------------------------------------------------------------
|
| This controller handles password setups for new users
|
*/
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
}
public function passwordset($token)
{
return view('users.passwordset')->with(['token' => $token]);
}
}
passwordset / {token} view
@extends('app')
@section('content')
<div class="container-fluid">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Set Password</div>
<div class="panel-body">
@if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form class="form-horizontal" role="form" method="POST" action="{{ url('/password/reset') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="token" value="{{ $token }}">
<div class="form-group">
<label class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input type="email" class="form-control" name="email" value="{{ old('email') }}">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Confirm Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password_confirmation">
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Set Password
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
答案 0 :(得分:4)
使用新电子邮件地址创建新帐户时,还需要在数据库中为该电子邮件添加唯一值,例如,您可以使用email
和{在数据库中创建记录{1}}字段因此在创建记录和存储token
时也存储令牌(唯一)。要获得唯一令牌,您可以尝试这样做:
email
现在将记录保存在数据库中并发送带有链接的电子邮件,链接可能如下所示:
$token = hash_hmac('sha256', str_random(40), config('app.key'));
现在,只需为链接创建路线,例如:
http://example.com/set/password/the-token-you-created-for-this-user
现在,在控制器中,声明方法,例如:
get('/set/password/{token}', 'PasswordSetupController@getSetPassword');
创建表单处理方法,例如:
public function getSetPassword($token)
{
// find the token from the database
// if you can find a record, for example:
$model = SomeModel::whereToken($token)->first();
if($model) {
// The matching $token is found. So show a view to set the password
// with a form textbox and submit button, set form action, add route
// for that action. Also, add a hidden field in the form for token,
// so you can check it again on form submission
}
}
路线可能是:
public function postSetPassword()
{
if($token = Input::get('hidden_token_field')) {
// Match it again and if matches then save the password and delete
// the hashed record or update the hashed field, just figure it out.
}
}
这是一个抽象的想法,但你应该能够完成所需的工作。我告诉你你要的想法。希望它能帮到你。确保根据上面的路线设置表单方法post('/set/password', 'PasswordSetupController@postSetPassword');
。