Ok, I've read up various posts such as CakePHP 3.x - AuthComponent::user() in View but the solution there doesn't tell you where to put that code so it will work everywhere in the application.
In CakePHP 2.x it was pretty straightforward to show things such a username in a navbar of a logged in user. The way this was done was by using the Auth component and then having something in the AppController to set a globally accessible variable with an array of the user details (first name, last name, etc).
In CakePHP 3.x I cannot see how this can be achieved.
Things such as $this->Auth->user('id');
only seem to work in Controllers.
I understand you could pass that from a Controller to a View. But what if you want it in a global template (e.g. default.ctp)? How do you do this in CakePHP 3.x? The documentation on Cake's website doesn't really tell you, because it only gives $this->Auth->user('id');
as an example.
Access via session
$this->request->session()->read('User.name');
http://book.cakephp.org/3.0/en/development/sessions.html#accessing-the-session-object
Please note that Components are used in controllers and Helpers are used in templates. There's no in build Auth helper.
Since all the logged in Auth user data is stored within session, and there's both Session helper and Session component, we can use the former in templates.
$user = $this->request->session()->read('Auth.User');
This will return all the details of logged in user in this format:
Array(
[id] => 1
[username] => abc
[email] => abc@xyz.com
.......
)
Reference: Accessing the Session Object