Cakephp 4 -身份验证插件如何获取当前用户名?

zz2j4svz  于 2022-11-11  发布在  PHP
关注(0)|答案(1)|浏览(152)

我正在使用CMS身份验证插件
我这样获取当前用户名,但当我更新用户配置文件时,当前用户名没有反映出来,我需要先注销

$user = $this->request->getAttribute('identity');
    $name = $user->first_name ?? '';
klr1opcd

klr1opcd1#

如何@ 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'));
}

相关问题