Getting user information in nav-top bar


Hi, Newbie here, but programming for 35 years.

I have loaded the cakedc/users plugin and he admin LTE theme. I have users that login. I want the current user name and information to appear in the top nav, not the hard coded alex user.

I have copied the template to src/Templates/Element/nav-top.ctp I can edit the template and changes take effect. I tried $this->User->last_name but it produces no output. just to test that I have the correct format $this->User->logout() produces the logout link. How do I get individual fields?


You have to pass user info to view layer from your AppController, e.g.

public function beforeRender(Event $event)

    $this->set('userInfo', $this->Auth->user())

Then you can use $userInfo var in your templates.


Thanks, Sort of works but not really.

in src/Controller/AppContoller.php I added what is past the JPF comment

public function beforeRender(Event $event)
if (!array_key_exists(’_serialize’, $this->viewVars) &&
in_array($this->response->type(), [‘application/json’, ‘application/xml’])
) {
$this->set(’_serialize’, true);
$this->set(‘theme’, Configure::read(‘Theme’));
// JPF July 17 2017
$this->set(‘userInfo’, $this->Auth->user());

In src/Template/Element/nav-top.ctp I used

<?php echo $userinfo["username"]; ?>

The website barfed this out

Notice (8): Undefined variable: userinfo [APP/Template/Element/nav-top.ctp, line 112]

Despite the variable being defined in the debugging panel.


userinfo vs userInfo


Oh, egg on face. Spelling counts. Thanks for your patience.