我正在使用 Laravel 并尝试像使用 java 一样使用 try/catch。不幸的是,它没有按预期工作......异常没有被捕获,并且没有返回错误消息,而是产生了 422 异常。
这是我的功能:
public function changePassword(Request $request){
try{
if (!(Hash::check($request->get('currentpassword'), Auth::user()->password))) {
return "Your current password does not matches with the password you provided. Please try again.";
}
if(strcmp($request->get('currentpassword'), $request->get('new-password')) == 0){
return "New Password cannot be same as your current password. Please choose a different password.";
}
$validatedData = $request->validate([
'currentpassword' => 'required',
'newpassword' => 'required|string|min:6',
]);
$user = Auth::user();
$user->password = bcrypt($request->get('newpassword'));
$user->save();
return "Password changed successfully !";
}
catch(Exception $error){
return $error->getMessage();
}
}
我这样称呼这个方法
Route::post('memberform/changePassword','MemberController@changePassword')->name('changePassword');
在这里,我想获取我的异常消息并显示它。相反,我在使用我的请求时遇到错误,并且未捕获到此异常
422 Unprocessable Entity {"message":"给定的数据无效。","errors":{"newpassword":["新密码必须至少为6个字符。"]}}
有只小跳蛙
缥缈止盈