首页 文章

Laravel注销无法正常工作

提问于
浏览
0

我在laravel 5.2中尝试了简单的注销功能,但是我真的不明白我错在哪里 . 有人可以提供帮助会很棒 .

这是 Route

Route::get('logout', 'loginController@getLogout');

loginController getLogout 方法:

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

link 在视图中使用此功能:

<a href="{{url('logout')}}">Logout</a>

session store 代码:

$request->session()->put('name', $username['name']);

AuthController 构造函数:

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

当用户单击注销链接时,它会重定向到根页面,但不会真正破坏会话或注销 . 它不需要登录查看页面(它应该) .

2 回答

  • 0

    尝试使用以下方法更改routes.php中的路由:

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

    对于我使用的注销路线:

    {{ url('/logout') }}
    

    通常情况下这是有效的,如果您需要使用不同的控制器进行特殊操作,请尝试使用:

    $request->session()->flush()
    

    在控制器中 . 遵循Laravel 5.2文档 - > https://laravel.com/docs/5.2/session .

    其他近似值,尝试修改控制器中的顺序,也许它会起作用 . 根据文档,Auth:logout()将清除所有用户auth数据,然后您可以清理其他会话数据 .

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

    我也有同样的问题,我已经通过方法1进行了纠正,并且我使用方法2进行了参考 .

    Method 1:

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

    Method 2: 或在AuthController构造函数中添加

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

    希望这样可以清除你的错误 . 我有同样的问题,我确实喜欢这个

    Session Destroy must be used like this

    Session::forget('name');
    $request->session()->flush(); // in your Controller
    

相关问题