Laravel控制器代码修改后刷新不生效及参数类型错误问题
Laravel控制器代码修改不生效及参数类型错误解决方案
一、参数类型错误修复
错误原因:generate方法中错误地将Request实例变量$request覆盖为字符串类型的邮箱地址,导致传递给generateOtp的参数不符合方法声明的类型要求;同时generateOtp内部逻辑也存在调用错误。
修复后的代码示例(推荐方案):
public function generate(Request $request) { // 验证请求数据 $request->validate([ 'reset_mailid' => 'required|exists:vs360_tables,mailid' ]); // 单独提取邮箱地址,不覆盖原Request实例 $resetMailId = $request->input('reset_mailid'); // 调用生成OTP的方法,传递邮箱字符串 $verificationCode = $this->generateOtp($resetMailId); $message = "Your OTP To Login is :- ".$verificationCode->otp; // 重定向至OTP验证页面 return redirect()->route('otp.verification', ['reset_mailid' => $verificationCode->reset_mailid])->with('success', $message); } // 修改方法参数为字符串类型,匹配传入的邮箱值 public function generateOtp(string $resetMailId) { $user = User::where('mailid', $resetMailId)->first(); // 查询用户是否存在未过期的OTP记录 $verificationCode = VerificationCode::where('reset_mailid', $user->reset_mailid)->latest()->first(); $now = Carbon::now(); if($verificationCode && $now->isBefore($verificationCode->expire_at)){ return $verificationCode; } // 创建新的OTP记录 return VerificationCode::create([ 'reset_mailid' => $resetMailId, 'otp' => rand(123456, 999999), 'expire_at' => Carbon::now()->addMinutes(10) ]); }
二、代码修改后不生效的解决步骤
- 清除Laravel各类缓存,在项目根目录执行命令:
php artisan cache:clear php artisan route:clear php artisan config:clear php artisan view:clear - 若开启了PHP OPcache,重启本地服务器(如XAMPP/WAMP),或在开发环境关闭OPcache以实时加载代码。
- 确认修改的是目标路径下的文件:
D:\localserver\htdocs\Laravel_Projects\final_vserve360\app\Http\Controllers\AuthOtpController.php,避免修改了错误副本。
内容的提问来源于stack exchange,提问作者Naveen kumar
相关产品推荐
相关产品推荐

