【问题标题】:The POST method is not supported for this route. Supported methods: GET, HEAD. - Laravel [duplicate]此路由不支持 POST 方法。支持的方法:GET、HEAD。 - Laravel [重复]
【发布时间】:2021-01-21 03:13:11
【问题描述】:

我是 Laravel 的新手。我正面临 Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException 此路由不支持 POST 方法。支持的方法:GET、HEAD。 提交表单时出错。请帮我解决。

我的编码是……

student_edit.balde.php

    <!DOCTYPE html>
<html>
<head>
<title>Sample view</title>
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
    <form action="images_update" method="post">
        {{ csrf_field() }}
    <div class="container">
        <div class="row">
            <div class="com-md-4">
            <div class="form-group">
                <input type="hidden" name="id" value="{{ $student['id'] }}">
                <label>Name:</label> 
                <input type="text" name="name" placeholder="Enter the name" class="form-control"
                value="{{ $student['name'] }}">
            </div>
            <div class="form-group">
                <label>Email:</label>
                <input type="email" name="email" placeholder="Enter the email" class="form-control"
                value="{{ $student['email'] }}">
            </div>
            <div class="form-group">  
                <label>Mobile:</label>
                <input type="text" name="mobile" placeholder="Enter the mobile number" class="form-control"
                value="{{ $student['mobile'] }}">
            </div>
            <div class="form-group"> 
                <input class="btn btn-success" type="submit" name="submit" value="Update">
            </div>
        </div>
    </div>
    </div>
    
</form>

</body>
</html>

web.php

    <?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

use App\Http\Controllers\TestController;

Route::get('/images', [TestController::class, 'index']);
Route::post('/images', [TestController::class, 'store']);
Route::get('/images/{id}', [TestController::class, 'show']);
Route::post('/images_update', [TestController::class, 'update']);

【问题讨论】:

标签: php laravel laravel-8


【解决方案1】:

如上所述,尝试将表单操作行更改为以下内容:

<form action="/images_update" method="post">

还请注意,您可以在 Laravel 中使用 Named Routes(我在下面提供了它们的用法示例)。这些为您提供了对路由助手的访问,这将避免您手动输入每个页面的 URL。

student_edit.blade.php

<!DOCTYPE html>
<html>
   <head>
      <title>Sample view</title>
      <link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
   </head>
   <body>
      <form action="{{ route('images.update') }}" method="post">
      {{ csrf_field() }}
      <div class="container">
         <div class="row">
            <div class="com-md-4">
               <div class="form-group">
                  <input type="hidden" name="id" value="{{ $student['id'] }}">
                  <label>Name:</label> 
                  <input type="text" name="name" placeholder="Enter the name" class="form-control"
                     value="{{ $student['name'] }}">
               </div>
               <div class="form-group">
                  <label>Email:</label>
                  <input type="email" name="email" placeholder="Enter the email" class="form-control"
                     value="{{ $student['email'] }}">
               </div>
               <div class="form-group">  
                  <label>Mobile:</label>
                  <input type="text" name="mobile" placeholder="Enter the mobile number" class="form-control"
                     value="{{ $student['mobile'] }}">
               </div>
               <div class="form-group"> 
                  <input class="btn btn-success" type="submit" name="submit" value="Update">
               </div>
            </div>
         </div>
      </div>

web.php

<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

use App\Http\Controllers\TestController;

Route::get('/images', [TestController::class, 'index'])->name('images.home');
Route::post('/images', [TestController::class, 'store'])->name('images.store');
Route::get('/images/{id}', [TestController::class, 'show'])->name('images.show');
Route::post('/images_update', [TestController::class, 'update'])->name('images.update');

【讨论】:

    猜你喜欢
    • 2020-06-05
    • 2020-04-19
    • 2021-05-08
    • 2020-01-22
    • 2020-04-25
    • 1970-01-01
    • 2019-08-28
    • 2019-08-31
    相关资源
    最近更新 更多