Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating Logout routes for Laravel 9.0x

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 <div class="row" >
  <div class="col-12">
   <div class="card">
    <div class="card-body">
     <nav class="breadcrumb">
      <a class="breadcrumb-item active " href="dashboard/{id}" >Dashboard</a>
      <a class="breadcrumb-item" href="about-us">About us</a>
      <a class="breadcrumb-item" href="products">Products</a>
      <a class="breadcrumb-item" href="contact-us">Contact Us</a>
     </nav>
    </div>
   </div>
  </div>
 </div>
 <li>
  <a href="logout" class="btn waves-effect waves-light btn-danger"  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

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading