Laravel 9 html表单提交抛出405。期望PUT,请求是GET
我正在尝试使用 Laravel 9 创建一个简单的 CRUD 应用程序。我遇到了 HTML 表单的问题。我创建了一个页面,您可以在其中编辑数据库中现有的兔子数据。
我的表单
<form name="editRabbitForm" action="{{ url('/rabbit/update') }}" method="PUT">
{{ csrf_field() }}
<!-- here be input fields -->
<button type="submit" class="btn btn-success">Save</button>
<a type="button" href="/rabbits" class="btn btn-danger">Cancel</a>
</form>
web.php
路线
<?php
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\QuoteController;
use App\Http\Controllers\RabbitController;
use Illuminate\Support\Facades\Route;
Route::get('/rabbits', [RabbitController::class, 'index']);
Route::get('/rabbit/edit/{id}', [RabbitController::class, 'edit']);
Route::put('/rabbit/update', [RabbitController::class, 'update']);
RabbitController.php
<?php
namespace App\Http\Controllers;
use App\Models\Rabbit;
use Illuminate\Http\Request;
class RabbitController extends Controller
{
public function index() {
return view('rabbits.index', ['rabbits' => Rabbit::all()]);
}
public function edit($id) {
return view('rabbits.edit', ['rabbit' => Rabbit::where('breed_id', $id)->first()]);
}
public function update(Request $request) {
echo("SOME SORT OF RESULT!");
var_dump($request);
}
}
在我点击控制器之前我得到一个异常读数: 此路由不支持 GET 方法。支持的方法:PUT。
我真的不明白在这种情况下我做错了什么
I'm trying to create a simple CRUD application with Laravel 9. I've run into a problem with HTML forms. I've created a page where you can edit data on rabbits existing in my database.
My form
<form name="editRabbitForm" action="{{ url('/rabbit/update') }}" method="PUT">
{{ csrf_field() }}
<!-- here be input fields -->
<button type="submit" class="btn btn-success">Save</button>
<a type="button" href="/rabbits" class="btn btn-danger">Cancel</a>
</form>
web.php
routes
<?php
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\QuoteController;
use App\Http\Controllers\RabbitController;
use Illuminate\Support\Facades\Route;
Route::get('/rabbits', [RabbitController::class, 'index']);
Route::get('/rabbit/edit/{id}', [RabbitController::class, 'edit']);
Route::put('/rabbit/update', [RabbitController::class, 'update']);
RabbitController.php
<?php
namespace App\Http\Controllers;
use App\Models\Rabbit;
use Illuminate\Http\Request;
class RabbitController extends Controller
{
public function index() {
return view('rabbits.index', ['rabbits' => Rabbit::all()]);
}
public function edit($id) {
return view('rabbits.edit', ['rabbit' => Rabbit::where('breed_id', $id)->first()]);
}
public function update(Request $request) {
echo("SOME SORT OF RESULT!");
var_dump($request);
}
}
Before I even hit the controller I get an exception reading:The GET method is not supported for this route. Supported methods: PUT.
I really don't get what I'm doing wrong in this scenario
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如我的评论中所述:
要发送 put 请求,您需要将
method
更改为POST
并添加@method('PUT')
> 以您的形式。这将为该方法插入一个隐藏输入。然后 Laravel 会自动将此请求路由到路由文件中的 put 路由指定的方法。这是必需的,因为正如 @ADyson 所写,浏览器仅限于
GET
和POST
请求。最后但并非最不重要的一点是,浏览器或本例中的 HTML 表单都是愚蠢的。
也许有一天这种情况会改变,谁知道呢。
As stated above in my comment:
To send a put request you will need to change the
method
toPOST
and add@method('PUT')
in your form. This will insert a hidden input for the method. Laravel will then automatically route this request to the method specified by the put route in your routes file.This is needed because as @ADyson writes, browsers are limited to
GET
andPOST
request.And last but not least, browsers or in this case HTML forms are stupid.
Maybe someday this will be changed, who knows.