CakePHP: how to read user data in a session?

I want to collect the latest user information from a user model and store it in a session.

so i do it

// Before rendering function beforeRender () {

    if($this->Session->check('Auth.User')) {
        $this->User->recursive = -1;
        $currentUser = $this->User->read(null, $this->Session->read('Auth.User.id'));
        $this->set(compact('currentUser'));
    }

}

It works fine until the User model appears.

When I go to the pages, it gives me an error, because User-> read does not work, since this model is not attached there.

What is the best solution for?

  • I want to get the latest Loggedin user information on every page of the site.
  • The latter, not Auth.User, from the session, because - when I edit the profile content, such as a name or photo. it still refers to old data that was saved when the user logged in.
  • , , Stackoverflow.

alt text

?

+3
3
  • /app/app_controller.php.
  • beforeRender.
  • $uses.

book.

+4

, , ( User ). ?

<?php
class UsersController extends AppController {
    public function edit() {
        $userId = $this->Session->read('Auth.User.id');
        // populate form
        if (!$this->data) {
            $this->data = $this->User->read($userId, null);
            return;
        }
        // update user
        $saved = $this->User->save($this->data);
        if ($saved) {
            $user = $this->User->read(null, $this->User->id);
            $this->Session->write('Auth.User', $user['User']); // update session
            $this->Session->setFlash('Account details have been updated');
            return $this->redirect(array('action' => 'profile'));
        }
        $this->Session->setFlash('Please correct the validation errors below');
    }
}
?>

, - ( php cake, database).

+2

You can try loading the model into your beforeRender as follows: $this->load('User')this should load the model for you on each controller. http://book.cakephp.org/view/992/loadModel

+1
source

Source: https://habr.com/ru/post/1768944/


All Articles