如果用户直接尝试注销路由,我如何重定向到登录

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

此登录中的一切工作正常,但如果用户直接尝试注销路由

localhost:8000/注销 它显示以下错误

我想要一种方法,如果直接调用路由,我可以重定向到登录, 什么是它的背心方式。

在您的路线文件中定义这样的路线。这是因为注销只有post方法。

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 Whosebug before proceeding.

问题出在哪里?

错误消息不言自明。

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

解决方法是什么?

你可以为 logout 定义一个 GET 路线,如果你真的想这样做的话。

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');
});