我是laravel 5.4的新手,我正在尝试练习一些laravel的东西。我想在申请人登录之前注册并验证他/她的电子邮件时创建电子邮件验证功能。但是当我点击下面的链接以验证申请人的电子邮件地址时,我遇到了这个错误。我不知道我是否搞砸了我的路线并且很难解决这个问题。这是验证电子邮件地址http://127.0.0.1:8000/register/verify/KafEACL1U3DxoprDI0aIVpAyM3DY8Q的链接。
以下是我的路线中的代码:
Route::group(['prefix' => 'applicant', 'namespace' => 'Applicant'], function () {
Route::get('/', 'LoginController@showLoginForm');
Route::post('/login', 'LoginController@authenticate')->name('applicant.login');
Route::post('/logout', 'LoginController@logout')->name('logout');
Route::get('/home', 'HomeController@index')->name('applicant.home');
Route::get('/signup', 'LoginController@signup')->name('applicant.signup');
Route::post('/register_check', 'LoginController@store')->name('register_check');
Route::resource('applications', 'ApplicationController',
['as' => 'applicant']);
Route::get('/verify', 'LoginController@confirm')->name('applicant.verify');
//secured file view only by customer-relations
Route::get('applications/print/{application_number}', [
'as' => 'applicant.application-form.print',
'uses' => 'ApplicationController@printApplicationConfirmation',
]);
});
继承我控制器中的代码:
public function store(CreateApplicantRequest $request)
{
$input = $request->all();
$confirmation_code = str_random(30);
$data = array('confirmation_code'=>$confirmation_code);
$applicants = $this->applicantRepository->create([
'name' => $input['name'],
'email' => $input['email'],
'password' => bcrypt($input['password']),
'address' => $input['address'],
'cellphone_no' => $input['cellphone_no'],
'confirmation_code' => $confirmation_code
]);
Mail::send('applicant-dashboard.verify', compact('confirmation_code'), function($message) {
$message->to(Input::get('email'), Input::get('name'))
->subject('Verify your email address');
});
Flash::message('Thanks for signing up! Please check and verify your email.');
return redirect(url('applicant'));
}
public function confirm($confirmation_code)
{
if( ! $confirmation_code)
{
throw new InvalidConfirmationCodeException;
}
$applicant = Applicant::whereConfirmationCode($confirmation_code)->first();
if ( ! $user)
{
throw new InvalidConfirmationCodeException;
}
$applicant->confirmed = 1;
$applicant->confirmation_code = null;
$applicant->save();
Flash::message('You have successfully verified your account.');
return Redirect::url('applicant');
}
答案 0 :(得分:2)
更改您的验证链接:
http://127.0.0.1:8000/register/verify/KafEACL1U3DxoprDI0aIVpAyM3DY8Q
到
http://127.0.0.1:8000/applicant/register/verify/KafEACL1U3DxoprDI0aIVpAyM3DY8Q
因为您的路线位于前缀为applicant
还会将验证路线编辑为:
Route::get('/register/verify/{confirmation_code}', 'LoginController@confirm')->name('applicant.verify');
不要忘记在命名空间行后面的控制器文件顶部使用此导入:
use App\Exceptions\InvalidConfirmationCodeException;
这是因为laravel使用psr-4
自动加载,这意味着InvalidConfirmationCodeException
异常类位于'app/Exceptions'
文件夹下。
答案 1 :(得分:2)
您的路线文件中没有路径注册/验证/ {confirmation_code}请添加如下:
Route::get('register/verify/{confirmation_code}', ['as' => 'applicant.verify' => 'uses' => 'LoginController@confirm');