该路由不支持POST方法。支持的方法:GET,HEAD。 -Laravel

时间:2020-10-06 08:52:35

标签: php laravel laravel-8

我是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']);

1 个答案:

答案 0 :(得分:2)

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

<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');