Laravel 注册时的电子邮件确认
Laravel Email Confirmation On Registration
我需要在创建新用户后发送邮件。
但我不知道如何 return 到主页而不出现错误。
这就是我现在正在做的事情。
User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'phone' => bcrypt($data['phone']),
'confirmation_code' => str_random(30),
]);
Email_function();
if (Auth::attempt(['email' => $data['email'], 'password' => bcrypt($data['password']) ])) {
// Authentication passed...
return redirect('/');
}
我一直收到这个错误消息。
SErrorException in SessionGuard.php line 439:
Argument 1 passed to Illuminate\Auth\SessionGuard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, null given, called in /Applications/XAMPP/xamppfiles/htdocs/sniddl/vendor/laravel/framework/src/Illuminate/Foundation/Auth/RegistersUsers.php on line 63 and defined
编辑:
更改标题以反映答案。
好的,事实证明,通过将 User::create
设置为变量,它允许您通过返回变量来登录用户。像这样。
$user = User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'phone' => bcrypt($data['phone']),
'confirmation_code' => str_random(30),
]);
Email_function();
return $user;
这是一个修改后的创建函数,为您的注册控制器添加了电子邮件功能
确保请求包含在使用命名空间的页面顶部:
use Illuminate\Http\Request;
更改控制器中的创建函数:
protected function create(Request $data)
{
$user = new User;
$user->name = $data->input('name');
$user->username = $data->input('username');
$user->email = $data->input('email');
$user->password = bcrypt($data->input('password'));
$user->phone = bcrypt($data->input('phone'));
$user->confirmation_code = str_random(60);
$user->save();
if ($user->save()) {
$this->sendEmail($user);
return redirect('VIEWPATH.VIEWFILE')->with('status', 'Successfully created user.');
} else {
return redirect('VIEWPATH.VIEWFILE')->with('status', 'User not created.');
}
}
在将使用 Laravels 内置电子邮件的同一控制器中创建 sendEmail 函数。确保您创建了 HTML 电子邮件:
public function sendEmail(User $user)
{
$data = array(
'name' => $user->name,
'code' => $user->confirmation_code,
);
\Mail::queue('EMAILVIEWPATH.HTMLEMAILVIEWFILE', $data, function($message) use ($user) {
$message->subject( 'Subject line Here' );
$message->to($user->email);
});
}
注意:
在上面的示例中,您需要至少更新 VIEWPATH.VIEWFILE
和 EMAILVIEWPATH.HTMLEMAILVIEWFILE
。
检查以下存储库中的 CONTROLLER:
https://github.com/laravel/laravel/blob/master/app/Http/Controllers/Auth/RegisterController.php
https://github.com/jeremykenedy/laravel-auth/blob/master/app/Http/Controllers/Auth/AuthController.php
注册视图(blade)示例
https://github.com/jeremykenedy/laravel-auth/blob/master/resources/views/auth/register.blade.php
接收变量的电子邮件视图:
https://github.com/jeremykenedy/laravel-auth/blob/master/resources/views/emails/activateAccount.blade.php
我需要在创建新用户后发送邮件。
但我不知道如何 return 到主页而不出现错误。 这就是我现在正在做的事情。
User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'phone' => bcrypt($data['phone']),
'confirmation_code' => str_random(30),
]);
Email_function();
if (Auth::attempt(['email' => $data['email'], 'password' => bcrypt($data['password']) ])) {
// Authentication passed...
return redirect('/');
}
我一直收到这个错误消息。
SErrorException in SessionGuard.php line 439:
Argument 1 passed to Illuminate\Auth\SessionGuard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, null given, called in /Applications/XAMPP/xamppfiles/htdocs/sniddl/vendor/laravel/framework/src/Illuminate/Foundation/Auth/RegistersUsers.php on line 63 and defined
编辑: 更改标题以反映答案。
好的,事实证明,通过将 User::create
设置为变量,它允许您通过返回变量来登录用户。像这样。
$user = User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'phone' => bcrypt($data['phone']),
'confirmation_code' => str_random(30),
]);
Email_function();
return $user;
这是一个修改后的创建函数,为您的注册控制器添加了电子邮件功能
确保请求包含在使用命名空间的页面顶部:
use Illuminate\Http\Request;
更改控制器中的创建函数:
protected function create(Request $data)
{
$user = new User;
$user->name = $data->input('name');
$user->username = $data->input('username');
$user->email = $data->input('email');
$user->password = bcrypt($data->input('password'));
$user->phone = bcrypt($data->input('phone'));
$user->confirmation_code = str_random(60);
$user->save();
if ($user->save()) {
$this->sendEmail($user);
return redirect('VIEWPATH.VIEWFILE')->with('status', 'Successfully created user.');
} else {
return redirect('VIEWPATH.VIEWFILE')->with('status', 'User not created.');
}
}
在将使用 Laravels 内置电子邮件的同一控制器中创建 sendEmail 函数。确保您创建了 HTML 电子邮件:
public function sendEmail(User $user)
{
$data = array(
'name' => $user->name,
'code' => $user->confirmation_code,
);
\Mail::queue('EMAILVIEWPATH.HTMLEMAILVIEWFILE', $data, function($message) use ($user) {
$message->subject( 'Subject line Here' );
$message->to($user->email);
});
}
注意:
在上面的示例中,您需要至少更新 VIEWPATH.VIEWFILE
和 EMAILVIEWPATH.HTMLEMAILVIEWFILE
。
检查以下存储库中的 CONTROLLER: https://github.com/laravel/laravel/blob/master/app/Http/Controllers/Auth/RegisterController.php https://github.com/jeremykenedy/laravel-auth/blob/master/app/Http/Controllers/Auth/AuthController.php
注册视图(blade)示例 https://github.com/jeremykenedy/laravel-auth/blob/master/resources/views/auth/register.blade.php
接收变量的电子邮件视图: https://github.com/jeremykenedy/laravel-auth/blob/master/resources/views/emails/activateAccount.blade.php