
在 Laravel 中,HTML 表单原生仅支持 GET 和 POST 方法;若需使用 PUT、DELETE 等 RESTful 方法,必须通过 _method 隐藏字段配合 POST 提交来模拟,否则会报“GET method is not supported”错误。
在 laravel 中,html 表单原生仅支持 get 和 post 方法;若需使用 put、delete 等 restful 方法,必须通过 `_method` 隐藏字段配合 post 提交来模拟,否则会报“get method is not supported”错误。
Laravel 路由严格遵循 HTTP 方法语义,Route::put() 仅响应 PUT 请求,但浏览器 <form></form> 标签的 method 属性不支持 put 或 delete 值(HTML5 规范仅允许 get/post/dialog)。因此,即使你在 Blade 中写 method="put",浏览器仍会以 GET 方式发起请求(或忽略该值后退化为 GET),导致 Laravel 路由匹配失败并抛出 “The GET method is not supported for this route” 错误。
✅ 正确做法是:始终使用 method="POST",再配合 @method('PUT') 或 @method('DELETE') 指令。Laravel 的 MethodServiceProvider 会自动将携带 _method=PUT 的 POST 请求转换为真正的 PUT 请求。
修改你的 Blade 表单如下:
<form method="POST" action="{{ route('users.wipe') }}">
@csrf
@method('PUT')
<button type="submit" class="btn btn-danger">Delete Account</button>
</form>⚠️ 注意事项:
-
@method('PUT')必须与@csrf同时存在,且位于<form></form>内部; -
action建议使用route()辅助函数(如route('users.wipe'))而非硬编码路径,确保 URL 正确且可维护; - 路由定义保持不变(
Route::put('users/wipe', ...)),无需修改; - 对于删除操作,语义更准确的做法是使用
DELETE方法(RESTful 最佳实践):
// web.php
Route::delete('users/wipe', [UserController::class, 'del'])->name('users.wipe');<!-- 对应表单 -->
<form method="POST" action="{{ route('users.wipe') }}">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">Delete Account</button>
</form>同时,建议在控制器中增强安全性:添加确认逻辑、软删除(SoftDeletes)、或二次验证(如密码确认),避免误操作导致不可逆数据丢失。例如:
public function del(Request $request)
{
$user = Auth::user();
// 可选:验证当前密码(提升安全性)
if (!Hash::check($request->input('password'), $user->password)) {
return back()->withErrors(['password' => 'The provided password does not match.']);
}
Auth::logout();
$user->delete(); // 或 $user->forceDelete() 若禁用软删除
return redirect()->route('welcome')->with('status', 'Your account has been deleted.');
}总结:Laravel 的 @method 是解决 HTML 表单方法限制的核心机制;牢记「表单用 POST + @method,路由定义对应动词」这一模式,即可正确处理 PUT/DELETE 等非标准表单方法。


















