Laravel 4:如何制作确认邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18482338/
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
Laravel 4: how to make confirmation email?
提问by FrancescoMussi
I have made until now an app with login/register and it works fine. After the registration a welcome email is sent.
到目前为止,我已经制作了一个带有登录/注册功能的应用程序,它运行良好。注册后会发送欢迎电子邮件。
But what i would like to do is to send a link, within that mail, that only after clicking on it, it is possible to login.
但我想要做的是在该邮件中发送一个链接,只有在点击它后,才有可能登录。
Like the common registration email for forum etc..
比如论坛常用的注册邮箱等。
Someone can help me please?
有人可以帮我吗?
This is the postRegister method:
这是 postRegister 方法:
public function postRegister()
{
$input = Input::all();
$rules = array(
'username' => 'required',
'password' => 'required');
$validation = Validator::make($input, $rules);
if ($validation->passes()) {
$password = $input['password'];
$password = Hash::make($password);
$user = new User;
$user->username = $input['username'];
$user->email = $input['email'];
$user->password = $password;
$mailer = new Mailers\UserMailer($user);
// var_dump($mailer);
$mailer->welcomeMail()->deliver();
$user->save();
return Redirect::to('afterRegister');
}
return Redirect::back()->withInput()->withErrors($validation)->with('message', 'Validation Errors!');
}
Thank you
谢谢
回答by ciruvan
Here are a few clues (not gonna write the code for you).
这里有一些线索(不会为您编写代码)。
- Add two fields to your user table:
confirmation
,confirmed
. - Create a route in Laravel like
registration/verify/{confirmation}
, in which you try and find a user in your DB with the given confirmation code (if found, set user'sconfirmed
field to 1). - Upon user registration, generate a unique confirmation code (you can use the
str_random()
helper function for this). - Set DB entry of new user accordingly (
confirmation
= the random code,confirmed
= 0) - Include a verification link (built according to your verification route) with the generated confirmation code in your email to your new user.
- 向您的用户表中添加两个字段:
confirmation
,confirmed
。 - 在 Laravel 中创建一个类似 的路由
registration/verify/{confirmation}
,在该路由中,您尝试使用给定的确认代码在您的数据库中查找用户(如果找到,将用户的confirmed
字段设置为 1)。 - 用户注册后,生成唯一的确认码(您可以
str_random()
为此使用辅助函数)。 - 相应地设置新用户的数据库条目(
confirmation
= 随机代码,confirmed
= 0) - 在发送给新用户的电子邮件中包含带有生成的确认码的验证链接(根据您的验证路径构建)。
Auth attempts can now be done like this:
现在可以像这样进行身份验证尝试:
$user = array(
'username' => Input::get('username'),
'password' => Input::get('password'),
'confirmed' => 1
);
if (Auth::attempt($user)) {
// success!
return Redirect::route('restricted/area');
}