Laravel 5.4 - 单击电子邮件中的验证链接时未传递或发送电子邮件验证令牌令牌。
Posted
技术标签:
【中文标题】Laravel 5.4 - 单击电子邮件中的验证链接时未传递或发送电子邮件验证令牌令牌。【英文标题】:Laravel 5.4 - Email Verification Token token not being passed or sent when clicking verification link in the email. 【发布时间】:2018-09-14 10:32:01 【问题描述】:我只想将验证邮件发送给管理员注册的用户。只有管理员可以注册新用户。但是每次我注册时都会发送电子邮件但是当您单击验证链接时会出现 routecollection 错误我猜想链接时单击未通过或发送每次在系统中注册新用户时生成的令牌。
用户控制器 用户控制器.php 命名空间 App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Mail;
use App\Mail\verifyEmail;
//imageuplod
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\DB;
class UserController extends Controller
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
$users = User::all();
//Load all users on the table and pass the users
$users = User::where(['archive'=>1])->orderBy('id')->get();
return view('usercrud.index')->with('users', $users);
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create(Request $request)
//
$usersC = new User;
$url = '';
$usersC->position = $request->companyPos;
$usersC->empid = $request->employeeid;
$usersC->name = $request->fullname;
$usersC->email = $request->email;
$usersC->password = bcrypt($request->password);
$usersC->roles = $request->role_id;
//token and status for image upload
$usersC->verifyToken = Str::random(40);
$usersC->status = 0;
//image uplod
if(Input::hasFile('images'))
$file = Input::file('images');
$file->move(public_path(). '/uploads/',
$file->getClientOriginalName());
$url = '/uploads/'. $file->getClientOriginalName();
$usersC-> image = $url;
$usersC -> save();
$users = User::all();
$users = User::where(['archive'=>1])->orderBy('id')->first();
//email verification
$thisUser = User::findOrFail($users->id);
$this->sendEmail($thisUser);
//$users = User::where(['archive'=>1])->orderBy('id')->get();
return redirect()->to('userIndex')->with('users', $users);
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
//
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
//
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
//
$users = User::where(['id'=>$id])->first();
// dd($users);
return view('usercrud.edit',compact('users'));
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
//
$usersUp = new User;
$url = '';
$password = bcrypt($request->editpassword);
if(Input::hasFile('images'))
$file = Input::file('images');
$file->move(public_path(). '/uploads/', $file-
>getClientOriginalName());
$url = '/uploads/'. $file->getClientOriginalName();
// dd($request->editcompanyPos);
$usersUp = User::where('id',$id)
->update(['position'=>$request-
>editcompanyPos,'empid'=>$request->editemployeeid,
'name'=>$request->editfullname,'email'=>$request-
>editemail,'password'=>$password,'roles'=>$request->editrole_id,
'image'=>$url]);
$users = User::all();
$users = User::where(['archive'=>1])->orderBy('id')->get();
return redirect()->to('userIndex')->with('users', $users);
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
$userDel = User::where('id',$id)->update(['archive'=>0]);
$users = User::all();
$users = User::where(['archive'=>1])->orderBy('id')->get();
return redirect()->to('userIndex')->with('users', $users);
public function verifyEmailFirst(Request $request)
//
return view('usercrud.verifyEmailFirst');
public function sendEmail($thisUser)
Mail::to($thisUser['email'])->send(new verifyEmail($thisUser));
public function sendEmailDone($email, $verifyToken)
$user = User::where(['email'=>$email, 'verifyToken'=>$verifyToken])-
>first();
//return $user;
if ($user)
return user::where(['email'=>$email,
'verifyToken'=>$verifyToken])-
>update(['status'=>'1','verifyToken'=>NULL]);
else
return 'user not found';
邮件 verifyEmail.php - laravel 中的邮件功能
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\User;
class verifyEmail extends Mailable
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public $user;
public function __construct(User $user)
//
$this->user=$user;
/**
* Build the message.
*
* @return $this
*/
public function build()
return $this->view('usercrud.sendView');
路线 web.php - 路由
Route::get('/', function ()
return view('bgcbus');
);
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/home/submitsurvey',
['uses'=>'HomeController@submitsurvey','as'=>'surveysubmit']);
Route::resource('/users', 'UserController');
//UserCreate
Route::post('/create','UserController@create');
Route::get('/userIndex','UserController@index');
//User Update
Route::match(['get','post'],'/userUpdate/id','UserController@update');
//User Edit
Route::get('/userEdit/id','UserController@edit');
//User Delete
Route::get('/userDelete/id','UserController@destroy');
//Bus
Route::resource('/buses', 'BusController');
//Bus Create
Route::post('/buscreate','BusController@create');
Route::get('/busindex','BusController@index');
//Bus Edt
Route::get('/busEdit/id','BusController@edit');
//Bus Update
Route::match(['get','post'],'/busUpdate/id','BusController@update');
//Bus Delete
Route::get('/busDelete/id','BusController@destroy');
//Bus Routes
Route::resource('/bgcroutes', 'BgcrouteController');
//Bus Route create
Route::post('/bgcroutecreate', 'BgcrouteController@create');
Route::get('/bgcrouteindex', 'BgcrouteController@index');
//Bus Route Edit
Route::get('/bgcrouteEdit/id','BgcrouteController@edit');
//Bus Route update
Route::match(['get','post'],'/bgcrouteUpdate/id',
'BgcrouteController@update');
//Bus Route Delete
Route::get('/bgcrouteDelete/id','BgcrouteController@destroy');
//Questions
Route::resource('/questions', 'SurveyquestionController');
//Questions create
Route::post('/questioncreate', 'SurveyquestionController@create');
Route::get('/questionindex', 'SurveyquestionController@index');
//Questions Edit
Route::get('/questionEdit/id','SurveyquestionController@edit');
//Questions update
Route::match(['get','post'],'/questionUpdate/id',
'SurveyquestionController@update');
//Questions Delete
Route::get('/questionDelete/id','SurveyquestionController@destroy');
//Applying Active Value
Route::post('/question/apply',
// Selecting Emoji
Route::get('/home/surveyvote',
['uses'=>'HomeController@surveyvote','as'=>'surveyvoting']);
//2ndQuestions
Route::resource('/scndquestions', 'QuestionsurveyController');
//2ndQuestions create
Route::post('/scndquestionscreate', 'QuestionsurveyController@create');
Route::get('/scndquestionsindex', 'QuestionsurveyController@index');
//2ndQuestions Edit
Route::get('/scndquestionsEdit/id','QuestionsurveyController@edit');
//2ndQuestions update
Route::match(['get','post'],'/scndquestionsUpdate/id',
'QuestionsurveyController@update');
//2ndQuestions Delete
Route::get('/scndquestionsDelete/id','QuestionsurveyController@destroy');
//Applying Active Value in 2nd Question
Route::post('/scndquestion/setapply', ['uses' =>
'QuestionsurveyController@setapply','as'=>'setapplyscndQuestion']);
//Dashboard
Route::get('/driverDashboard', 'DashboardfirstController@index');
Route::post('/driverDashboard/data',
Route::get('/driverDashboardAggregated',
'DashboardfirstController@secondindex');
Route::post('/piechartaggregated',
['uses'=>'ChartsController@getaggregated','as'=>'getaggregateddata']);
//History
Route::get('/history', 'HistoryController@index');
//verification email
Route::get('verify/email/verifyToken','UserController@sendEmailDone')
->name('sendEmailDone');
homecontroller - 处理帐户登录等的控制器
<?php
namespace App\Http\Controllers;
use App\Answer;
use App\User;
use Auth;
use Illuminate\Http\Request;
use Controller\UserController;
class HomeController extends Controller
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
$this->middleware('auth');
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
$users = User::where('ID', Auth::id())->first();
//if ($users->status==1)
if ($users->roles == 1)
return view('admins');
elseif ($users->roles == 2)
return view('dispatcher');
return view('survey');
//
// return view('usercrud.verifyEmailFirst');
// return view('home');
function surveyvote(Request $request)
Answer::insert([
'driverid'=>$request->driverid,
'busid'=>$request->busid,
'routeid'=>$request->routeid,
'questionid'=>$request->questionid,
'responses'=>$request->responseid,
'created_at'=>date('Y-m-d h:i:s')
]);
return 'Successful';
User.php
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Str;
class User extends Authenticatable
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'position', 'roles', 'verifyToken'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
观看次数
Sendview.blade.php
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
csrf_field()
TO veerify <a href=" route('sendEmailDone', ["email" => $user-
>email,"verifyToken"=>$user->verifyToken]) "> click here </a>
</body>
</html>
sendView.blade.php
<h1>verify email first <a href=""> back </a></h1>
【问题讨论】:
可以显示错误信息吗! 【参考方案1】:你只需要像这样改变你的sendEmailDone
函数:-
public function sendEmailDone($email, $verifyToken)
$user = User::where(['email'=>$email,'verifyToken'=>$verifyToken])->first();
$user->status = 1;
$user->verifyToken = Null;
if($user->save())
return view('xyz_view',['user'=>$user]);
else
return 'user not found';
【讨论】:
错误仍然存在。尝试更新 sendEmailDone 方法所在的 Usercontroller 代码,但我仍然遇到同样的问题。以上是关于Laravel 5.4 - 单击电子邮件中的验证链接时未传递或发送电子邮件验证令牌令牌。的主要内容,如果未能解决你的问题,请参考以下文章