Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

onurzdgn's avatar

logout

Hello everyone, I’m using laravel 10. I am trying logout. This is my web.php

Route::controller(LoginController::class)->group(function () {
    Route::get('login', 'index');
    Route::post('authenticate', 'authenticate')->name('authenticate');
    Route::get('logout', 'logout')->name('logout');
});

This is my controller:

    public function logout(Request $request): RedirectResponse
    {
        Auth::logout();
        $request->session()->invalidate();
        $request->session()->regenerateToken();
        $request->session()->flush();
 
        return redirect('login');
    }

And this is my html:

<a href="/logout" class="nav_link" onclick="event.preventDefault(); document.getElementById('logout-form').submit();">
            <i class='bx bx-log-out nav_icon'></i>
            <span class="nav_name">Çıkış</span>
</a>
<form id="logout-form" action="{{ url('logout') }}" method="get" class="d-none">@csrf</form>

This is not working. But when I can change web.php like this:

Route::get('logout', function () {
    Auth::logout();
    return redirect('login');
})->name('logout');

And use same html code it is work. My question why it is not working and how I can fix it?

0 likes
16 replies
ramonrietdijk's avatar

Are you sure the controller method is hit?

Also, you should be able to remove $request->session()->flush(); from your code, the invalidate method already flushes the session.

Snapey's avatar

You shouldn't use a GET route for a logout function. It should be a POST for best security practice.

2 likes
Snapey's avatar

check if you have middleware in the __construct method of your LoginController

onurzdgn's avatar

@Snapey It is stil not work I use this format and work

Route::get('login', [LoginController::class, 'index'])->name('login');
Route::post('authenticate', [LoginController::class, 'authenticate'])->name('authenticate');
Route::post('logout', function () {
    Auth::logout();
    return redirect('login');
})->name('logout');
Snapey's avatar

@onurzdgn get back to the previous question. WHAT middleware in the controller? Why are you making this harder than it needs to be?

onurzdgn's avatar

@Snapey this middleware

public function __construct()
    {
        $this->middleware(['guest']);
    }
Snapey's avatar
Snapey
Best Answer
Level 122

@onurzdgn See, now we are getting somewhere

so the functions in this controller can only be accessed BY GUESTS.

A logged in user, wanting to logout is NOT a guest

change the line to read

$this->middleware(['guest'])->except('logout');

and then your controller method should work.

gadhyare's avatar

change form method to POST

public function logout(Request $request)
{
    Auth::logout();

    $request->session()->invalidate();
    $request->session()->regenerateToken();

    return to_route('admin.login');

}
Snapey's avatar

@gadhyare that was said, its best practice, but it also was not the problem.

Please or to participate in this conversation.