简体   繁体   English

Laravel 8:此路由不支持 GET 方法。 支持的方法:POST

[英]Laravel 8: The GET method is not supported for this route. Supported methods: POST

I'm working with Laravel 8 to develop my project and I have built this form for verifying token:我正在与 Laravel 8 合作开发我的项目,我已经构建了这个表单来验证令牌:

<div class="card-body">
    <form action="{{ route('profile.2fa.phone') }}" method="POST">
        @csrf

        <div class="form-group">
            <label for="token" class="col-form-label">Token</label>
            <input type="text" class="form-control @error('token') is-invalid @enderror" name="token" placeholder="enter your token">
            @error('token')
                <span class="invalid-feedback">
                <strong>{{ $message }}</strong>
            </span>
            @enderror
        </div>
        <div class="form-group">
            <button class="btn btn-primary">Validate token</button>
        </div>
    </form>
</div>

And then at web.php I have these routes:然后在web.php我有这些路线:

Route::get('/settings/twofactor/phone' , [App\Http\Controllers\Admin\AdminSettingsController::class, 'getPhoneVerify']);

Route::post('/settings/twofactor/phone' , [App\Http\Controllers\Admin\AdminSettingsController::class, 'postPhoneVerify'])->name('profile.2fa.phone');

But now when I add the token and press the Validate Token button, I get this error:但是现在,当我添加令牌并按下“验证令牌”按钮时,出现此错误:

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException The GET method is not supported for this route. Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException此路由不支持 GET 方法。 Supported methods: POST.支持的方法:POST。

So what is going wrong here?那么这里出了什么问题? How can I solve this issue?我该如何解决这个问题?

I would really appreciate any idea or suggestion from you guys...我真的很感激你们的任何想法或建议......

Thanks in advance.提前致谢。


Here are all routes:以下是所有路线:

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

Route::prefix('admin')->middleware(['auth', 'verified'])->group(function() {
    Route::get('/', function () {
        return view('admin.index');
    });

    Route::resource('users' , App\Http\Controllers\Admin\AdminUserController::class)->middleware('admin');

    Route::get('/settings', [App\Http\Controllers\Admin\AdminSettingsController::class, 'index'])->name('profile');

    Route::post('/settings/twofactor' , [App\Http\Controllers\Admin\AdminSettingsController::class, 'postManageTwoFactor'])->name('profile.2fa.manage');

    Route::get('/settings/twofactor/phone' , [App\Http\Controllers\Admin\AdminSettingsController::class, 'getPhoneVerify']);
    Route::post('/settings/twofactor/phone' , [App\Http\Controllers\Admin\AdminSettingsController::class, 'postPhoneVerify'])->name('profile.2fa.phone');

    Route::get('/auth/token' ,[App\Http\Controllers\Auth\AuthTokenController::class, 'getToken'])->name('2fa.token');
    Route::post('/auth/token' ,[App\Http\Controllers\Auth\AuthTokenController::class, 'postToken']);
});

Auth::routes(['verify' => true]);

Route::get('/auth/google', [App\Http\Controllers\Auth\GoogleAuthController::class, 'redirect'])->name('auth.google');
Route::get('/auth/google/callback', [App\Http\Controllers\Auth\GoogleAuthController::class, 'result']);

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

Auth::routes();

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

If you have routes like /users/posts/ , /users/posts/create , and /users/posts/edit then create and edit routes should be written before /users/posts to avoid route conflict problem.如果您有/users/posts//users/posts/create/users/posts/edit等路由,则应在/users/posts之前编写创建和编辑路由以避免路由冲突问题。

In your routes web.php , change the following routes order:在您的路线web.php中,更改以下路线顺序:

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

Route::prefix('admin')->middleware(['auth', 'verified'])->group(function() {
    Route::get('/', function () {
        return view('admin.index');
    });

    Route::resource('users' , App\Http\Controllers\Admin\AdminUserController::class)->middleware('admin');

    Route::get('/settings/twofactor/phone' , [App\Http\Controllers\Admin\AdminSettingsController::class, 'getPhoneVerify']);
    Route::post('/settings/twofactor/phone' , [App\Http\Controllers\Admin\AdminSettingsController::class, 'postPhoneVerify'])->name('profile.2fa.phone');

    Route::post('/settings/twofactor' , [App\Http\Controllers\Admin\AdminSettingsController::class, 'postManageTwoFactor'])->name('profile.2fa.manage');

    Route::get('/settings', [App\Http\Controllers\Admin\AdminSettingsController::class, 'index'])->name('profile');

    Route::get('/auth/token' ,[App\Http\Controllers\Auth\AuthTokenController::class, 'getToken'])->name('2fa.token');
    Route::post('/auth/token' ,[App\Http\Controllers\Auth\AuthTokenController::class, 'postToken']);
});

Auth::routes(['verify' => true]);

Route::get('/auth/google/callback', [App\Http\Controllers\Auth\GoogleAuthController::class, 'result']);

Route::get('/auth/google', [App\Http\Controllers\Auth\GoogleAuthController::class, 'redirect'])->name('auth.google');

Auth::routes();

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

There is no problem with your code.您的代码没有问题。 Clearing the cache will be helpful.清除缓存会很有帮助。 Add additional method.添加其他方法。 would you try this way你会这样试试吗

<div class="card-body">
<form action="{{ route('profile.2fa.phone') }}" method="POST">
    @csrf
    @method("POST")

    <div class="form-group">
        <label for="token" class="col-form-label">Token</label>
        <input type="text" class="form-control @error('token') is-invalid @enderror" name="token" placeholder="enter your token">
        @error('token')
            <span class="invalid-feedback">
            <strong>{{ $message }}</strong>
        </span>
        @enderror
    </div>
    <div class="form-group">
        <button class="btn btn-primary">Validate token</button>
    </div>
</form>
</div>

I think your problem will solve if you try this one, just change the following line of your code with this one.我认为如果您尝试这个,您的问题就会解决,只需用这个更改代码的以下行。

<button type="submit" class="btn btn-primary">Validate token</button>

button type="submit" class="btn btn-dark">sent This button solve your problem put this button in your form button type="submit" class="btn btn-dark">sent This button solve your problem 把这个按钮放在你的表单中

The error message "The GET method is not supported for this route" typically occurs when you try to access a route using the GET method, but the route definition only allows the POST method.当您尝试使用 GET 方法访问路由,但路由定义仅允许 POST 方法时,通常会出现错误消息“此路由不支持 GET 方法”。 In your case, the route /settings/twofactor/phone is defined to allow only the POST method, as shown in your code:在您的情况下,路由 /settings/twofactor/phone 被定义为仅允许 POST 方法,如您的代码所示:

Route::post('/settings/twofactor/phone', [App\Http\Controllers\Admin\AdminSettingsController::class, 'postPhoneVerify'])->name('profile.2fa.phone');

However, in your form, you're using the POST method correctly:但是,在您的表单中,您正确使用了 POST 方法:

<form action="{{ route('profile.2fa.phone') }}" method="POST">

To resolve the issue, you need to make sure that you're accessing the route with the correct HTTP method.要解决此问题,您需要确保使用正确的 HTTP 方法访问路由。 Since your route only allows the POST method, you need to submit the form using the POST method.由于您的路由只允许 POST 方法,因此您需要使用 POST 方法提交表单。

If you're still encountering the error, there might be other factors causing the issue.如果您仍然遇到该错误,则可能是其他因素导致了该问题。 Here are a few things you can check:您可以检查以下几项:

Clear your application's route cache by running the following command in your terminal:通过在终端中运行以下命令来清除应用程序的路由缓存:

php artisan route:clear

Make sure there are no conflicting route definitions that could be causing the issue.确保没有可能导致问题的冲突路由定义。

Ensure that there are no middleware restrictions or other route-related logic that may be interfering with the request.确保没有可能干扰请求的中间件限制或其他与路由相关的逻辑。

By addressing these points, you should be able to resolve the "The GET method is not supported for this route" error.通过解决这些问题,您应该能够解决“此路由不支持 GET 方法”错误。

Yes,.是的,。 changing to below worked for me.更改为以下对我有用。 Submit Form提交表格

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 此路由不支持 GET 方法。 支持的方法:POST Laravel 8 - The GET method is not supported for this route. Supported methods: POST Laravel 8 此路由不支持 GET 方法。 支持的方法:POST。 在 laravel 8 - The GET method is not supported for this route. Supported methods: POST. in laravel 8 Laravel: MethodNotAllowedHttpException: 此路由不支持 GET 方法。 支持的方法:POST - Laravel: MethodNotAllowedHttpException: The GET method is not supported for this route. Supported methods: POST Laravel - 此路由不支持 POST 方法。 支持的方法:GET、HEAD - Laravel - The POST method is not supported for this route. Supported methods: GET, HEAD 此路由不支持 GET 方法。 支持的方法:POST。 与 laravel - The GET method is not supported for this route. Supported methods: POST. with laravel 此路由不支持 POST 方法。 支持的方法:GET、HEAD (LARAVEL) - POST method is not supported for this route. Supported methods: GET, HEAD (LARAVEL) 此路由不支持 GET 方法。 支持的方法:POST。 拉拉维尔 8 - The GET method is not supported for this route. Supported methods: POST. laravel 8 Laravel此路由不支持GET方法。 支持的方法:POST - Laravel The GET method is not supported for this route. Supported methods: POST Laravel 6:此路由不支持 GET 方法。 支持的方法:POST 错误 - Laravel 6: The GET method is not supported for this route. Supported methods: POST Error 此路由不支持 POST 方法。 支持的方法:GET、HEAD In Laravel - The POST method is not supported for this route. Supported methods: GET, HEAD In Laravel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM