简体   繁体   中英

How can i redirect to login if user tries logout route directly

Everything in this login is working fine but if a user tries logout route directly

localhost:8000/logout it shows the following error

在此处输入图像描述

i want a way so that i can redirect to login if the route is called directly, what could be the vest way for it.

define a route like this in your routes file. It is because only post method exist for logout.

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

A word of caution: As Laurel mentioned in this comments, using GET request for Logout is a bad idea, Read this discussion on StackOverflow before proceeding.

Where is the problem?

The error message is self explanatory.

The logout route doesn't support HTTP Method GET , but POST .

What is the solution?

You can define a GET route for logout , if you really wants to do it.

Route::get('logout', function() {
   // Do whatever you want to do with Logout
  Auth::logout(); // logout the user
  return redirect()->route('login'); // redirect the user to login page
});
Route::get('check_logout','ExampleController@check_logout');
Route::get('logout',function(){
    return redirect('login');
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM