【问题标题】:CakePHP 3 refresh session dataCakePHP 3 刷新会话数据
【发布时间】:2016-01-01 04:26:01
【问题描述】:

我有一个页面,我可以在其中修改用户详细信息(用户名、名字、头像...)。 我的导航栏中有一个元素,其中包含有关当前登录用户的信息。问题是我不知道如何在修改数据后立即刷新会话。

UsersController中:

public function edit($id = null)
{
    if (!$id) {
        throw new NotFoundException(__('Invalid user'));
    }

    $user = $this->Users->get($id);
    if ($this->request->is(['post', 'put'])) {
        $this->Users->patchEntity($user, $this->request->data);
        if ($this->Users->save($user)) {

            //REFRESH SESSION ????\\
            $this->request->session()->write('Auth.User', $user);
            //\\

            $this->Flash->success(__('User has been updated.'));
            return $this->redirect(['action' => 'edit/' . $id]);
        }
        $this->Flash->error(__('Unable to update User details.'));
    }

    $this->set(compact('user'));
}

【问题讨论】:

    标签: php session cakephp cakephp-3.0


    【解决方案1】:

    只需按照登录时的设置方式进行更新,即使用AuthComponent::setUser()

    此外,您可能只想在已编辑的用户实际上是当前登录的用户的情况下这样做。并且您希望将数据设置为与 Auth 组件相同的格式,即 (现在),作为一个数组,为了安全起见,没有密码。

    假设有一个名为id 的单列主键和一个名为password 的密码列

    if ($this->Auth->user('id') === $user->id) {
        $data = $user->toArray();
        unset($data['password']);
    
        $this->Auth->setUser($data);
    }
    

    另见

    【讨论】:

    • unset($data['password']) 应该不是必需的,因为它应该在您的实体内的隐藏属性中。
    • @StefanD。在理想的情况下,烘焙功能直到一个月前才被破坏(烘焙 1.1.4),人们甚至可能不使用烘焙,和/或使用除tokenpasswordpasswd(这是 bake 检测到的),所以我想说仍然值得一提的是人们应该注意这一点。
    猜你喜欢
    • 2016-01-20
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2019-01-16
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    • 2021-10-01
    相关资源
    最近更新 更多