Laravel9+Firebase环境更新邮箱/密码失败:$user未定义变量报错
Laravel 9 + Firebase 个人资料更新功能报错问题
问题概述
基于Laravel 9结合Firebase/Firestore开发的系统,登录、邮箱重置密码、注册功能正常,但更新邮箱/密码功能失效:
- 报错提示
$user为未定义变量 update方法中的return $input也提示变量未定义- 代码复制自视频教程,此前功能正常,整合UI后失效;暂用
ProfileController对应settings视图,未迁移至SettingsController - 已尝试Stack Overflow相关方案(因技术栈差异无效),执行过
php artisan cache:clear和php artisan route:clear,问题仍未解决
相关代码
web.php 路由配置
Route::resource('/home/profile', App\Http\Controllers\Auth\ProfileController::class)->middleware('user','fireauth');
settings.blade.php 视图代码
{!! Form::model($user, ['method'=>'PATCH', 'action'=> ['App\Http\Controllers\Auth\ProfileController@update',$user->uid]]) !!} {!! Form::open() !!} <div class="form-group px-3"> {!! Form::label('email', 'Email ',['class'=>'pt-3 col-12 text-left pl-0']) !!} {!! Form::email('email', null, ['class'=>'col-md-8 form-control'])!!} </div> <div class="form-group px-3"> {!! Form::label('new_password', 'New Password:',['class'=>'col-12 text-left pl-0']) !!} {!! Form::password('new_password', ['class'=>'col-md-8 form-control'])!!} </div> <div class="form-group px-3"> {!! Form::label('new_confirm_password', 'Confirm Password:',['class'=>'col-12 text-left pl-0']) !!} {!! Form::password('new_confirm_password', ['class'=>'col-md-8 form-control'])!!} </div> {!! Form::submit('Save', ['class'=>'btn btn-primary']) !!} {!! Form::close() !!}
ProfileController.php 控制器代码
public function index() { $uid = Session::get('uid'); $user = app('firebase.auth')->getUser($uid); return view('admin.settings',compact('user')); } public function update(Request $request, $id) { $auth = app('firebase.auth'); $user = $auth->getUser($id); try { if ($request->new_password == '' && $request->new_confirm_password =='') { $request->validate([ 'displayName' => 'required|min:3|max:12', 'email' =>'required', ]); $properties =[ 'displayName' => $request->displayName, 'email' => $request->email, ]; $updatedUser = $auth->updateUser($id, $properties); if ($user->email != $request->email) { $auth->updateUser($id, ['emailVerified'=>false]); } Session::flash('message', 'Profile Updated'); return back()->withInput(); } else { $request->validate([ 'new_password' => 'required|max:18|min:8', 'new_confirm_password' => 'same:new_password', ]); $updatedUser = $auth->changeUserPassword($id, $request->new_password); Session::flash('message', 'Password Updated'); return back()->withInput(); } return $input; } catch (\Exception $e) { Session::flash('error', $e->getMessage()); return back()->withInput(); } }
问题原因分析
- 视图表单结构错误:同时调用
Form::model()和Form::open()生成嵌套表单,导致提交逻辑异常;且视图缺少displayName输入框,与控制器中的验证规则冲突。 - 返回视图未传递
$user变量:update方法执行return back()->withInput()时,未重新传递$user变量,导致视图渲染时找不到该变量。 - 冗余无效代码:
update方法末尾的return $input中$input从未定义,属于无效代码。
修复方案
1. 修正视图表单代码
删除重复的Form::open()调用,补充displayName输入框(若需更新用户名):
{!! Form::model($user, ['method'=>'PATCH', 'action'=> ['App\Http\Controllers\Auth\ProfileController@update',$user->uid]]) !!} <div class="form-group px-3"> {!! Form::label('displayName', 'Display Name ',['class'=>'pt-3 col-12 text-left pl-0']) !!} {!! Form::text('displayName', $user->displayName, ['class'=>'col-md-8 form-control'])!!} </div> <div class="form-group px-3"> {!! Form::label('email', 'Email ',['class'=>'pt-3 col-12 text-left pl-0']) !!} {!! Form::email('email', $user->email, ['class'=>'col-md-8 form-control'])!!} </div> <div class="form-group px-3"> {!! Form::label('new_password', 'New Password:',['class'=>'col-12 text-left pl-0']) !!} {!! Form::password('new_password', ['class'=>'col-md-8 form-control'])!!} </div> <div class="form-group px-3"> {!! Form::label('new_confirm_password', 'Confirm Password:',['class'=>'col-12 text-left pl-0']) !!} {!! Form::password('new_confirm_password', ['class'=>'col-md-8 form-control'])!!} </div> {!! Form::submit('Save', ['class'=>'btn btn-primary']) !!} {!! Form::close() !!}
2. 修正控制器update方法
移除无效代码,重定向至index方法(自动传递$user变量),异常捕获时重新传递$user:
public function update(Request $request, $id) { $auth = app('firebase.auth'); try { if ($request->new_password == '' && $request->new_confirm_password =='') { $request->validate([ 'displayName' => 'required|min:3|max:12', 'email' =>'required|email', ]); $properties =[ 'displayName' => $request->displayName, 'email' => $request->email, ]; $updatedUser = $auth->updateUser($id, $properties); if ($updatedUser->email != $request->email) { $auth->updateUser($id, ['emailVerified'=>false]); } Session::flash('message', 'Profile Updated'); return redirect()->route('home.profile.index'); } else { $request->validate([ 'new_password' => 'required|max:18|min:8', 'new_confirm_password' => 'same:new_password', ]); $auth->changeUserPassword($id, $request->new_password); Session::flash('message', 'Password Updated'); return redirect()->route('home.profile.index'); } } catch (\Exception $e) { Session::flash('error', $e->getMessage()); $user = $auth->getUser($id); return view('admin.settings', compact('user'))->withInput(); } }
3. 补充路由命名(可选)
显式指定路由名称,方便重定向:
Route::resource('/home/profile', App\Http\Controllers\Auth\ProfileController::class) ->middleware('user','fireauth') ->names([ 'index' => 'home.profile.index', 'update' => 'home.profile.update', ]);
内容的提问来源于stack exchange,提问作者Overley Loley
相关产品推荐
相关产品推荐

