首页 文章

Laravel 5.2:Auth :: logout()不起作用

提问于
浏览
13

我正在Laravel 5.2中构建一个非常简单的应用程序,但是当使用 AuthController 's action to log out, it just simply doesn'工作时 . 我有一个导航栏,用于检查 Auth::check() ,并在调用注销操作后不会更改 .

我在routes.php文件中有这个路由:

Route::get('users/logout', 'Auth\AuthController@getLogout');

它在外面

Route::group(['middleware' => ['web']], function () 声明 .

我也尝试在AuthController.php文件的末尾添加跟随操作 .

public function getLogout() 
{
    $this->auth->logout();
    Session::flush();
    return redirect('/');
}

你有什么想法?

EDIT 1

如果我清除Google的Chrome缓存,则可以正常运行 .

8 回答

  • 0

    只需添加以下路由,不要在任何路由组(中间件)中添加:

    Route::get('your-route', 'Auth\AuthController@logout');
    

    现在注销应该在L 5.2中正常工作而不修改 AuthController 中的任何内容 .

  • 0
    /**
     * Log the user out of the application.
     *
     * @param \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function logout(Request $request)
    {
        $this->guard()->logout();
    
        $request->session()->flush();
    
        $request->session()->regenerate();
    
        return redirect('/');
    }
    
    /**
     * Get the guard to be used during authentication.
     *
     * @return \Illuminate\Contracts\Auth\StatefulGuard
     */
    protected function guard()
    {
        return Auth::guard();
    }
    
  • 6

    我在Laravel 5.2中也有类似的问题 . 你应该改变你的路线

    Route::get('auth/logout', 'Auth\AuthController@logout');
    

    或者在AuthController构造函数中添加

    public function __construct()
    {
        $this->middleware('guest', ['except' => ['logout', 'getLogout']]);
    }
    

    这对我有用 .

  • 2

    使用下面的代码

    Auth::logout();
    

    要么

    auth()->logout();
    
  • 1

    问题来自AuthController构造函数中的'guest'中间件 . 它应该从 $this->middleware('guest', ['except' => 'logout']); 更改为 $this->middleware('guest', ['except' => 'getLogout']);

    如果检查内核文件,可以看到您的访客中间件指向 \App\Http\Middleware\RedirectIfAuthenticated::class

    此中间件检查用户是否已通过身份验证,如果已通过身份验证,则会将用户重定向到根页,但如果未经过身份验证,则允许用户执行操作 . 通过使用 $this->middleware('guest', ['except' => 'getLogout']); ,调用getLogout函数时将不会应用中间件,从而使经过身份验证的用户可以使用它 .

    N / B:与原始答案一样,您可以将 getLogout 更改为 logout ,因为getLogout方法只是在laravel的实现中返回logout方法 .

  • 37

    Http->Middleware->Authenticate.php 中将 login 更改为 /

    return redirect()->guest('/');
    

    并在routes.php中定义以下路由

    Route::get('/', function () {
        return view('login');
    });
    

    用于退出调用以下功能:

    public function getlogout(){
        \Auth::logout();
        return redirect('/home');
    }
    

    Important: 重定向到 /home 而不是首先调用 $this->middleware('auth');/ 然后在中间件重定向到 /

  • 4

    这应该是AuthController中构造函数的内容

    $this->middleware('web');
    $this->middleware('guest', ['except' => 'logout']);
    
  • 0

    在routes.php文件中添加此行Route :: get('auth / logout','Auth \ AuthController @ getLogout');并在你的视图中添加一个href =“{{url('/ auth / logout')}}”>退出它对我来说很好

相关问题