1.用composer安装toplan/laravel-sms
composer require toplan/laravel
2.在config/app.php文件中修改别名
3.生成两个文件或者从其他项目拿过来
拿过来也行:
4.控制器引入
use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception;
5.代码逻辑
$validator = Validator::make($request->all(), [ 'mobile' => 'required|confirm_mobile_not_change|confirm_rule:mobile_required', 'verifyCode' => 'required|verify_code', ], [ 'mobile.required'=>"手机号不能为空", 'mobile.confirm_mobile_not_change'=>"手机号已变更,请重新发送验证码", 'mobile.confirm_rule'=>"手机号不能为空", 'verifyCode.required'=>"验证码不能为空", 'verifyCode.verify_code'=>"验证码错误", ]); if ($validator->fails()) { $message=$validator->messages(); //把对象转换成数组 $message = json_decode(json_encode($message),true); if ( !empty($message['mobile'])&& !empty($message['verifyCode']) ) { $error = $message['mobile'][0]; } if( empty($message['mobile'])&& !empty($message['verifyCode'])){ $error = $message['verifyCode'][0]; } if( !empty($message['mobile'])&& empty($message['verifyCode'])){ $error = $message['mobile'][0]; } //return redirect()->route("change_password")->with("msg",$error); return response()->json(['state'=>1,'error'=>$error]); }else{ //到这证明发送成功 数据存数据库 }