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.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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?
@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.
Please or to participate in this conversation.