Laravel 9 html表单提交抛出405。期望PUT,请求是GET

发布于 2025-01-14 12:19:29 字数 1841 浏览 4 评论 0原文

我正在尝试使用 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。

Laravel 定义的 URL 请求

我真的不明白在这种情况下我做错了什么

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.

URL request as defined by Laravel

I really don't get what I'm doing wrong in this scenario

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥醉 2025-01-21 12:19:29

正如我的评论中所述:

要发送 put 请求,您需要将 method 更改为 POST 并添加 @method('PUT') > 以您的形式。这将为该方法插入一个隐藏输入。然后 Laravel 会自动将此请求路由到路由文件中的 put 路由指定的方法。

这是必需的,因为正如 @ADyson 所写,浏览器仅限于 GETPOST 请求。

最后但并非最不重要的一点是,浏览器或本例中的 HTML 表单都是愚蠢的。
也许有一天这种情况会改变,谁知道呢。

As stated above in my comment:

To send a put request you will need to change the method to POST 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 and POST request.

And last but not least, browsers or in this case HTML forms are stupid.
Maybe someday this will be changed, who knows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文