【问题标题】:Larvel 8 POST not supported for this route error此路由错误不支持 Laravel 8 POST
【发布时间】:2020-11-17 10:56:29
【问题描述】:

此路由不支持 POST 方法。支持的方法:GET、HEAD。使用 Laravel 8 错误,我在这里做错了什么?

我的表单文件名 -- insertemp.blade.php

<form method="POST" action="/showemployee">
   @csrf
   <div class="form-group" style="margin-left: 30px">
      <label for="id">ID</label>
      <input class="form-control"  type="number" name="eid" id="empid"><br>
   </div>
</form>

我的web.php

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

Route::get('/showemployee',[EmployeeController::class,'show']);
Route::post('/showemployee',[EmployeeController::class,'store']);     

我的控制器文件 -- EmployeeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Employee;

class EmployeeController extends Controller
{
   public function show()
   {
      $emp = Employee::all();

      return view('showemployee',['employees' => $emp]);
   }

   public function store()
   {
       echo "I am in store";
       error_log(request('eid'));

       return redirect('/');
   }
}

【问题讨论】:

  • 为什么不用资源控制器?
  • @dılosürücü 我是 laravel 新手,我正在学习他使用基本控制器的教程
  • 好吧,看看我的答案,我共享代码示例

标签: php laravel eloquent


【解决方案1】:
Route::post('/showemployee', [EmployeeController::class,'store'])->name('form.post');

<form method="POST" action="{route('form.post')}}" >
    @csrf
    <div class="form-group" style="margin-left:30px">
       <label for="id">ID</label>
       <input class="form-control" type="number" name="eid" id="empid">
    </div>
</form>

如果还是不行

检查:

php artisan route:list

【讨论】:

  • Thanx 伙计,它起作用了,我的路线也没有更新,这也导致了一些问题,我之前尝试过这种方法,现在使用并运行命令 php artisan route:cache。
  • 是的,我可以再插入一个花括号,因为它在表单操作字段中丢失。好像有人提到他们不应该再想出一个障碍
  • 你能解释一下为什么正常路径在 POST 的情况下不起作用
  • laravel 全局路由助手生成 URI 路径……其实你可以手动给出但不推荐,可能会因为你必须使用全局路由助手而改变端口或域或文件夹等……
  • 好吧,主要是出于安全原因,我们可以说,而且如果将来我们需要更改所有相同路由的路径,我猜只需要更改一条路径可能很容易。
猜你喜欢
  • 2021-03-22
  • 1970-01-01
  • 2021-08-05
  • 2021-01-10
  • 1970-01-01
  • 2020-12-05
  • 2021-03-17
  • 2021-10-07
相关资源
最近更新 更多