User auth info from view?

Am I correct that the only way to access user info (for a logged in user) is by invoking the session? i.e.:

<?= $this->request->session()->read('Auth.User.name');?>

This seems kind of absurd as user data is the type of thing that frequently gets used in views for lots of different reasons. Also, it’s not documented under either the Auth section or the blog tutorial. Instead the suggestion is that you can access Auth directly.

So this is not a question so much as a vague plea for some kind of future enhancement to address this.

Unless there already is some shortcut and I just didn’t find it?

The best way to access controller variables is by setting them from inside the AppControllers beforeFilter, i.e.:

$this->set('loggedIn', (bool)$this->Auth->user());
$this->set('thisUser', $this->Auth->user());

Then you can just access the User auth array in your views through $thisUser[‘id’] etc. :smiley:

3 Likes

You’re a font of helpful and practical knowledge!