Laravel修改密码功能遇MethodNotAllowedHttpException错误求助
Laravel 修改密码功能 MethodNotAllowedHttpException 问题解决
问题描述
学习Laravel开发用户修改密码功能时,点击登录页面的“忘记密码”链接,触发如下错误:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException.The GET method is not supported for this route. Supported methods: POST。
已尝试清除缓存,问题仍存在。
相关代码
web.php
Route::get('/userPasswordChange', 'HomeController@changePassword')->name('userPasswordChange'); Route::post('/userPasswordChange', 'HomeController@changePasswordStore')->name('change.password');
HomeController.php
public function changePassword(){ return view('userPasswordChange'); } public function changePasswordStore(Request $request) { $request->validate([ 'current_password' => ['required', new MatchOldPassword], 'new_password' => ['required'], 'new_confirm_password' => ['same:new_password'], ]); User::find(auth()->user()->id)->update(['password'=> Hash::make($request->new_password)]); return redirect()->route('change.password')->with('success','Thay đổi mật khẩu thành công.'); }
userPasswordChange.blade.php
@extends('user.layouts.master') @section('main-content') <div class="container"> <div class="row"> <div class="col-md-12"> <div class="card"> <div class="card-header">Đổi mật khẩu</div> <div class="card-body"> <form method="POST" action="{{ route('change.password') }}"> @csrf @foreach ($errors->all() as $error) <p class="text-danger">{{ $error }}</p> @endforeach <div class="form-group row"> <label for="password" class="col-md-4 col-form-label text-md-right">Mật khẩu hiện tại</label> <div class="col-md-6"> <input id="password" type="password" class="form-control" name="current_password" autocomplete="current-password"> </div> </div> <div class="form-group row"> <label for="password" class="col-md-4 col-form-label text-md-right">Mật khẩu mới</label> <div class="col-md-6"> <input id="new_password" type="password" class="form-control" name="new_password" autocomplete="current-password"> </div> </div> <div class="form-group row"> <label for="password" class="col-md-4 col-form-label text-md-right">Xác nhận mật khẩu mới</label> <div class="col-md-6"> <input id="new_confirm_password" type="password" class="form-control" name="new_confirm_password" autocomplete="current-password"> </div> </div> <div class="form-group row mb-0"> <div class="col-md-8 offset-md-4"> <button type="submit" class="btn btn-primary"> Cập nhật mật khẩu </button> </div> </div> </form> </div> </div> </div> </div> </div> @endsection
问题原因
- 登录页面的“忘记密码”链接错误指向了仅支持POST请求的
change.password路由,GET访问该路由必然触发MethodNotAllowed错误。 - 控制器修改密码成功后,重定向目标同样是POST类型的
change.password路由,重定向属于GET请求,也会导致错误。
解决方案
1. 修正登录页面的链接
将“忘记密码”链接指向GET类型的修改密码页面路由:
<a href="{{ route('userPasswordChange') }}">忘记密码</a>
2. 修正控制器的重定向目标
修改changePasswordStore方法的重定向,指向GET路由:
return redirect()->route('userPasswordChange')->with('success','Thay đổi mật khẩu thành công.');
3. (可选)优化路由命名(避免混淆)
修改POST路由的名称,让命名更符合RESTful规范:
Route::post('/userPasswordChange', 'HomeController@changePasswordStore')->name('change.password.store');
同时更新视图中的表单提交地址:
<form method="POST" action="{{ route('change.password.store') }}">
内容的提问来源于stack exchange,提问作者ChaMuc
相关产品推荐
相关产品推荐

