Home > Software engineering >  Creating Logout routes for Laravel 9.0x
Creating Logout routes for Laravel 9.0x

Time:12-05

I can't make the logout link work for my simple laravel project. The user needs to login first before going to the dashboard, so I created a route that gets the {id} of the logged in user. and I think thats the reason the page just refreshes and does not logout and redirect to the login page. Ill provide the codes and snippets below.

Web.php file

Route::get('/dashboard/{id}',[CustomAuthController::class, 'dashboard'])->name('dashboard');
Route::get('/logout', [CustomAuthController::class, 'logout']);

Blade.php file

 <div  >
  <div >
   <div >
    <div >
     <nav >
      <a  href="dashboard/{id}" >Dashboard</a>
      <a  href="about-us">About us</a>
      <a  href="products">Products</a>
      <a  href="contact-us">Contact Us</a>
     </nav>
    </div>
   </div>
  </div>
 </div>
 <li>
  <a href="logout"   style="float:right;">Logout</a>
 </li>

Here is the link whenever i click the logout button

http://127.0.0.1:8000/dashboard/logout

Controller.php file for logout

    public function logout(){
        if(Session::has('loginId')){
            Session::pull('loginId');
            return redirect('login');
        }
    }

I tried googling other solutions but with no success. You might know some links I can read about or additional Laravel documentations. Thanks

CodePudding user response:

you're logout link is wrong change it from href="logout" to href="/logout" since its relative it has been trying to reach /dashboard/logout which is incorrect since the routing you've set is under /logout so it has only been redirected back to dashboard because of this

  • Related