首页 > 解决方案 > Laravel 5.7,在用户注销之前做一些事情

问题描述

我想type在注销之前将当前用户字段设置为其他内容,但问题是,我不知道应该将这段代码放在哪个控制器或哪个方法中。顺便说一句,我在 Laravel 5.7 中没有 AuthController。

$user = Auth::user();
$user->type = "something";
$user->save;

标签: phplaravel

解决方案


您可以为此使用 EventSubscriber。请查看文档。在句柄方法中,您将能够获取用户对象,例如:

public function hanlde($event) 
{
    $user = $event->user;
    // You will be able to change user here
}

其他方法:如果您查看 LoginController,您会看到它使用名为“AuthenticatesUsers”(Illuminate\Foundation\Auth\AuthenticatesUsers)的特征。这个特征有一个名为“注销”的方法

    /**
 * 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()->invalidate();

    return redirect('/');
}

您可以将其复制/粘贴到 LoginController 并重写它。PS:但我更喜欢第一种选择。但这取决于你。希望它会有所帮助。


推荐阅读