Reputation:
I am using cakephp 2.1. So I am looking for getting loggedIn user in views. How to get the logged user in views.
Upvotes: 0
Views: 285
Reputation: 4856
If you are using the Auth Component the user data is also stored in the Session's "Auth.User" key. So in a view it can be accessed with the SessionHelper::read() method:
$user = $this->Session->read("Auth.User");
Auth.User contains the user record from the database. Don't forget to include the Session Helper in the $helpers array in your controller.
Upvotes: 0
Reputation: 1844
If you want to show the current logged in user details to all views. it will be better to put the logic in the layout file rather than putting the code in all view files.
to get the current logged in user details you can use $this->Auth->user
Lets say if you want to display the current loged in user name you can use echo $this->Auth->user('user_name');
Upvotes: 0
Reputation: 87073
Try this in $this->Auth->loggedIn()
in your view. If it doesn't work then you have to use
$this->Auth->user('id')
.
For example;
if($this->Auth->loggedIn()) {
// then do something
}
or:
if($this->Auth->user('id')) {
// then do something
}
Upvotes: 0
Reputation: 11
You can take a look here:
or here:
http://api20.cakephp.org/class/auth-component#method-AuthComponentuser
Upvotes: 1