The GET method is not supported for this route

Viewed 186

I'm working with Laravel 8 to develop my project and I have built this form for verifying token:

<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:

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. Supported methods: 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.

2 Answers

First of all you need to check if your routes are being property created. Execute: php artisan route:list. If there is nothing about POST, execute this commands:

    php artisan config:cache // clear config cache
    php artisan config:clear // clear config cache
    php artisan cache:clear // clear cache
    php artisan optimize  // To reoptimize the class loader
    php artisan route:cache // clear route cache
    php artisan view:clear // clear view cache

In a easy way - php artisan optimize.

If that's did not help make sure that your route is route, that you are searching for.

UDP: name method is not working as you want. Try to declare name with out this method.

If you want, you can try to use this format of routes: Route::match(['get', 'post'], ...);

Laravel will match routes in the order they are declared.

To fix your problem you need to declare your Route::post before Route::get.

Your GET request should always be declared as the last method in your route list as it will trap any other request with a MethodNotAllowedHttpException.

Related