我这里有一个电子邮件验证代码,其中用户注册并在注册时接受注册确认电子邮件。我使用laravel make auth进行修改,包括将令牌发送到有效的电子邮件。注册成功后,应出现确认消息,但不会。
这里是代码:
RegisterController
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Mail;
use Illuminate\HTTP\Request;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
protected function register(Request $request){
$input = $request->all();
$validator = $this->validator($input);
if ($validator->passes()){
$data = $this->create($input)->toArray();
$data['token'] = str_random(25);
$user= User::find($data['id']);
$user->token=$data['token'];
$user->save();
Mail::send('mail.confirmation', $data, function($message) use($data){
$message->to($data['email']);
$message->subject('Registration Confirmation');
});
return redirect(route('login'))->with('status','confirmation email has been sent');
}
return redirect(route('login'))->with('status', $validator->errors);
}
public function confirmation($token)
{
$user = User::where('token', $token)->first();
if(!is_null($user)){
$user->confirmed=1;
$user->token='';
$user->save();
return redirect(route('login'))->with('status','complete actibeyt');
}
return redirect(route('login'))->with('status','something is wrong');
}
}
答案 0 :(得分:2)
名为Validator
的{{1}}上没有属性,但有一种方法。
简单的改变:
errors
答案 1 :(得分:0)
首先用这个替换你的返回函数: -
return redirect()->route('login')->with('status','confirmation email has been sent');
答案 2 :(得分:0)
看起来你已经解决了眼前的问题,但我创建了一个Web服务来解决这个特定的痛点,根本没有服务器端代码:clicktoverify.net。它只需要一个JS包含和几行代码:
<form method="POST" action="whatever" id="my-form">
<input type="email" name="email" />
<input type="submit" name="sign up!" />
</form>
<script type="text/javascript" src="https://cdn.clicktoverify.net/ctv.js"></script>
<script type="text/javascript">
form = document.getElementById("my-form");
form.addEventListener('submit', function(event) {
event.preventDefault();
alert("We've sent a verification email to this address. Please check your inbox.");
verifier = new CTV();
verifier.verify({
email: form['email'].value,
public_api_key: "71917c69-b8e8-3992-adf5-c2de3b89c17e", //you'll get your own when you create an account
success: function(xhr) {
alert("Your email address is verified!");
form.submit();
},
error: function(xhr) {
alert("Something went wrong! We couldn't verify your email address.");
}
});
});
</script>