3 回答
TA贡献1824条经验 获得超8个赞
您的错误处理代码是正确的。您用于捕获异常的代码是在 PHP 中执行此操作的方法,它的工作方式与在 Java 中的工作方式相同(我对两者都进行了编码)。简而言之,您的代码没有任何问题。
我的猜测是两件事之一,我不能 100% 确定其中任何一个:
1) 您正在 OSX 上进行测试,并且当大量嵌套事物时,某些 XDebug 设置可能会导致错误处理问题(我个人在迁移中遇到过这种情况)。 SO XDebug 设置问题
2) Laravel 有一个拦截器,可以在错误发生时捕获错误,并且该处理程序已被注入以优先于您的处理程序。 SO表单验证异常未捕获
希望这会推动您朝着正确的方向前进。对不起,这是一个非回答类型的答案。
TA贡献2041条经验 获得超4个赞
Laravel 验证失败不会抛出异常!!所以你不能赶上......如果你想赶上,使用下面的自定义验证并自己抛出异常
public function changePassword(Request $request)
{
try
{
$data['currentpassword'] = $request->get('currentpassword');
$data['newpassword'] = $request->get('newpassword');
if (!(Hash::check($request->get('currentpassword'), Auth::user()->password))) {
$message['currentpassword.required'] = "Your current password does not matches with the password you provided. Please try again.";
$data['currentpassword'] = ""; // I used for required rule as a example , but I recommend to create custom rule for this
}
if(strcmp($request->get('currentpassword'), $request->get('new-password')) == 0){
$message['newpassword.required'] = "New Password cannot be same as your current password. Please choose a different password.";
$data['newpassword'] = "";// I used for required rule as a example , but I recommend to create custom rule for this
}
$rule = [
'currentpassword' => 'required',
'newpassword' => 'required|string|min:6',
];
$validatedData = \Illuminate\Support\Facades\Validator::make($data, $rule, $message);
if($validateData->fails()) {
throw new \Exception($validateData->messages());
}
$user = Auth::user();
$user->password = bcrypt($request->get('newpassword'));
$user->save();
return "Password changed successfully !";
}
catch(Exception $error)
{
return $error->getMessage();
}
}
- 3 回答
- 0 关注
- 520 浏览
添加回答
举报