jhob101's avatar

The GET method is not supported for route logout. Supported methods: POST.

Laravel 10.7.1

This has been working in the application but has for some reason stopped.

It happens when the logout button is clicked. But that is definitely posting to the logout page:

<a class="dropdown-item" href="{{ route('logout') }}" onclick="e.preventDefault();
  document.getElementById('logout-form').submit();">
    {{ __('Logout') }}
    </a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" class="d-none">
@csrf
</form>

It shows as a POST route in route:list:

POST logout .............. logout › Auth\LoginController@logout

in web.php:

Auth::routes( [ 'register' => false ] );

I can see in the browser console it is indeed coming through as GET request, but how and why is that happening?

I'm completely stumped.

Any ideas?

0 likes
13 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

The issue is that the logout link is being clicked, which sends a GET request, instead of submitting the form, which sends a POST request. To fix this, you can modify the code to submit the form directly when the link is clicked, instead of relying on the link to submit the form. Here's an example:

<a class="dropdown-item" href="#" onclick="event.preventDefault(); document.getElementById('logout-form').submit();">
    {{ __('Logout') }}
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" class="d-none">
    @csrf
</form>

This code adds an onclick event to the link that prevents the default behavior (following the link), and instead submits the form directly. This ensures that a POST request is sent to the logout route, which is what the route expects.

alberane's avatar

Hi @jhob101 ...

Everything cool out there?

I have the same problem and I couldn't understand what it could be. The solution offered by the Artificial Intelligence BOT does not work in my case.

What did you do?

Thanks.

wightowl's avatar

Hi @alberane

The AI code didn't work for me either on Laravel 11.34.2.

In views/inc/navbar.blade.php I changed the standard logout code block that caused "The GET method is not supported for route logout. Supported methods: POST." for the following code which is working well so far:

<li>
    <form id="logout-form" action="{{ route('logout') }}" method="POST">
        @csrf
        <button class="dropdown-item" type="submit">
            Logout
        </button>
    </form>
</li>

In routes/auth.php the unchanged route is:

Route::post('logout', [AuthenticatedSessionController::class, 'destroy'])
    ->name('logout');
Snapey's avatar

The problem with using an anchor a tag and then overwriting the natural behaviour is that if there is a javascript error then the preventDefault has no effect and the link is followed as a GET.

The simplest solution is to change the a tag to a button (with type of button) and then if the javascript fails nothing happens.

3 likes
alberane's avatar

Damn!! I didn't think about that!!! Thank you very much!

SUNDAE-SOUP's avatar

I solved the same problem. What I did is to change the form method from "POST" into "GET" like this:

                    <form method="GET" action="{{ route('logout') }}">
                        @csrf

                        <x-dropdown-link :href="route('logout')"
                                onclick="event.preventDefault();
                                            this.closest('form').submit();">
                            {{ __('Log Out') }}
                        </x-dropdown-link>
                    </form>
psuplat's avatar

Well, I got something different. Using restfull api, my logout method invalidates all tokens.

My entry inside /routes/api.php:

Route::post('/logout', [AuthController::class, 'logout'])->name('logout');

Endpoint called via postman:

POST http://localhost:8000/api/logout

Getting this error:

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException: The GET method is not supported for route api/logout. Supported methods: POST. in file C:\Users\user\Projects\lara-api\vendor\laravel\framework\src\Illuminate\Routing\AbstractRouteCollection.php on line 122

Route is configured as POST, making a POST call from postman, and Laravel still claims I am using GET. Ideas why?

psuplat's avatar

@Snapey there is no redirection happening, not on Laravel side anyways. Laravel is just backend api consumed by standalone SPA app. The frontend call the laravel endpoint invalidating user tokens. And when this response returns success, then the frontend (in this case vuejs) clears all storage and redirect to login page.

I actually removed pretty much entire logic from that method, just returning a hardcoded respose with status 200, just to see if that works, but laravel insists that I am using a GET method, instead of POST which clearly is not true.

Currently my logout method looks like this:

public function logout(Request $request)
{
    return response()->json([
        'message' => 'Logged out successfully!',
        'status_code' => 200
    ], 200);
}

so what am I not seeing or understanding here?

ahmad7's avatar

@psuplat Hi PSUPLAT, I'm having the same problem if just use href, seem need to use form as example below

Good luck

111100001's avatar

well if any one is facing this issue, my dumba** had an extension called Web Optimizer that was causing the issue. i disabled it

Snapey's avatar

@111100001 what if your customer has that same extension?

Use a button with onclick instead of an <a element that your browser speedup tries to pre-fetch

Please or to participate in this conversation.