首页 > 解决方案 > 在 Laravel auth 中,该路由不支持 POST 方法。支持的方法:GET、HEAD

问题描述

我正在尝试在我的 laravel 8 项目中设置电子邮件验证,我已使用 auth 命令在我的项目中设置 mu 身份验证。我得到的错误是: -

[Route: verify.verify] [URI: email/verify/{id}] 缺少必需的参数。

这是我的 HomeController:-

public function __construct()
    {
        $this->middleware(['auth', 'verified']);
    }

这是我的 web.php:-(我确实有更多路线)

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\ConfessionsController;
use App\Http\Controllers\FriendsController;


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

Auth::routes();
Route::get('/home', [HomeController::class, 'index'])->name('home');
    
    //Confessions
Route::get('/confessions', [ConfessionsController::class, 'index'])->name('confession.index');
Route::get('/c/c/{id}', [ConfessionsController::class, 'create'])->name('confession.create');
Route::post('/confessions/created/{id}', [ConfessionsController::class, 'post'])->name('confession.store')->middleware('confessions');
Route::get('/confessions/delete/{id}', [ConfessionsController::class, 'destroy'])->name('confession.destroy');
    
    //friends
Route::post('/confessions/add/{id}', [FriendsController::class, 'store'])->name('friend.store')->middleware('friends');

通过阅读这个解决方案,我编辑了我的路线: -

use App\Http\Controllers\Auth\VerificationController;```

Auth::routes();

Route::get('email/verify', [VerificationController::class,'show'])->name('verification.notice');
Route::get('email/verify/{id}', [VerificationController::class,'verify'])->name('verification.verify');
Route::get('email/resend', [VerificationController::class, 'resend'])->name('verification.resend');

但我仍然遇到同样的错误。

这是我的 .env :-

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=MyUsername
MAIL_PASSWORD=MyPassword
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=MyEmailAddress
MAIL_FROM_NAME="${APP_NAME}"

用户.php:-

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use App\Models\Concerns\UsesUuid;
use App\Models\Confession;
use Webpatser\Uuid\Uuid;
use Cache;

class User extends Authenticatable implements MustVerifyEmail
{
    use HasFactory, Notifiable;


     protected $guarded = []; // YOLO



 public $incrementing = false;


  protected $keyType = 'string';

  protected static function boot()
  {
    parent::boot();
 self::creating(function ($user) {
     $user->uuid = (string) Uuid::generate(4);
 });
  }
}

更新:- 我已经protected $primaryKey="uuid";在我的用户模型中添加了,现在当我点击注册时,我没有收到任何错误以及没有电子邮件,但是当我点击时click here to request another,我收到了这个错误:-

此路由不支持 POST 方法。支持的方法:GET、HEAD。

标签: laravellaravel-routinglaravel-8laravel-authentication

解决方案


检查 app/Notifications/VerifyEmail 文件。

此通知使用用户模型的主键,默认模型假设“id”列是模型实例的主键。但是在您的模型中,主键是“uuid”,因此您必须将此行添加到您的模型中。

protected $primaryKey="uuid";

推荐阅读