CakePHP : Accessing the logged in user


Once a user is logged in, you will often need some particular information about the current user. You can access the currently logged in user using AuthComponent::user(). This method is static, and can be used globally after the AuthComponent has been loaded. You can access it both as an instance method or as a static method:

You can access all the user's information(fields) from the users table except the password.

<?php
// Use anywhere 
// Accesing user’s id
AuthComponent::user('id');                                                                
$_SESSION['Auth']['User']['id'];

// Accesing user’s username
AuthComponent::user('username');                                                                
$_SESSION['Auth']['User']['username'];

// Accesing user’s group_id
AuthComponent::user('group_id');                                                                
$_SESSION['Auth']['User']['group_id'];

// and so on…



// From inside the controller
// Accessing user's id
$this->Auth->User('id');

// Accessing user's username
$this->Auth->User('username');

// and so on…






Comments