php - Laravel 5.7,在用户注销之前做一些事情
问题描述
我想type
在注销之前将当前用户字段设置为其他内容,但问题是,我不知道应该将这段代码放在哪个控制器或哪个方法中。顺便说一句,我在 Laravel 5.7 中没有 AuthController。
$user = Auth::user();
$user->type = "something";
$user->save;
解决方案
您可以为此使用 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:但我更喜欢第一种选择。但这取决于你。希望它会有所帮助。
推荐阅读
- angular - Angular 服务单元测试 - 是否伪造嵌套方法?
- python-3.x - 如何在不知道文档位置的情况下删除所有同名文档 - Firestore
- java - 无法通过java中的构造函数实例化对象
- git - GitHub 与 Git Bash
- salesforce - 在 LWC 中嵌入带有闪电图标的闪电输入
- c++ - ReactiveX (rx) - 在对象上应用 observable 而不是在 observable 中创建对象
- javascript - 如何访问在 IF/ELSE 范围之外创建的变量、在 IF 语句中更新它并在 ELSE 语句中访问该更新?
- encryption - 将加密消息转换为字符串
- regex - WebStorm 使用 Regex 在找到的匹配项周围添加引号
- mongodb - MongoDB-减法