且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Laravel 5.3注销并重定向到登录页面?

更新时间:2023-09-28 23:24:04

在Laravel 5.4中测试

我认为最有效的解决方案是覆盖从 app/Http/Controllers/Auth/LoginController.php 文件内部调用的继承的注销"方法.为此,请向此类添加以下方法:

The solution that I believe works the best is overriding the inherited "logout" method that gets called from inside the app/Http/Controllers/Auth/LoginController.php file. Do this by adding the following method to this class:

/**
 * 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('/login');
}

由于"LoginController"类继承自 Illuminate \ Foundation \ Auth \ AuthenticatesUsers ,因此您应该可以安全地覆盖此方法(在LoginController中),而无需编辑实际的供应商文件本身...如果您想升级...,请编辑 AuthenticatesUsers 文件或任何供应商文件,这会引起很多麻烦.

As the "LoginController" class inherits from Illuminate\Foundation\Auth\AuthenticatesUsers, you should safely be able to override this method (in the LoginController) WITHOUT editing the actual vendor file itself... Editing the AuthenticatesUsers file, or any vendor file would cause major headaches down the road if you ever wanted to upgrade...

这里唯一的附加步骤是,您需要在 LoginController 类的顶部包括以下行:

The only additional step here is that you need to include the following line at the top of the LoginController class:

use Illuminate\Http\Request;