php - Cakephp 4 - 身份验证插件如何获取当前用户名?
问题描述
我正在使用 CMS 身份验证插件
我像这样获取当前用户名,但是当我更新用户配置文件时,当前用户名没有得到反映我需要先注销
$user = $this->request->getAttribute('identity');
$name = $user->first_name ?? '';
解决方案
如何@ CakePHP 4.x
更新身份对象:$this->Authentication->setIdentity($user);
完整示例:
public function editMe()
{
// fetch current identity data
$user = $this->Authentication->getIdentity()->getOriginalData();
if ($this->request->is(['patch', 'post', 'put'])) {
$user = $this->Users->patchEntity($user, $this->getRequest()->getData());
if ($this->Users->save($user)) {
$this->Authentication->setIdentity($user); // ----- > update identity data
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'me']);
}
$this->Flash->error(__('The user could not be saved. Please try again.'));
}
$this->set(compact('user'));
}
推荐阅读
- jquery - jQuery 隐藏论坛中的特定评论
- python - 引入高斯模糊后使用原始名称保存多张图像
- r - 使用 MST 包创建生存树:未定义的列错误?
- date - 连续运行的 SAS 开始和结束日期
- javascript - 在javascript中, (CONSTRUCTOR).prototype.constructor = (ANOTHER CONSTRUCTOR) 异步调用?
- python - 如何使用动态方法名
- jquery - jQuery的点击和函数执行之间的延迟
- javascript - 选择下拉菜单后,动态输入字段无法正确显示
- typescript - 使用列表初始化时,TypeScript List 不会验证元素的类型
- javascript - 有没有办法让猫鼬 findById 函数对已删除和不存在的 id 表现一致?