In CakePHP, it is sometimes necessary to access the Auth component from a view or a helper. For instance, you may want to use the Auth component to check if a user is currently logged-in to the system. By default, you cannot access access the Auth component from a view. So doing something like this in a view or helper:
$user = $this->Auth->user
will Generate an error in CakePHP since the Auth component cannot be accessed in a view or helper. Fortunately, there is a way to access the Auth component from a view or helper: you can simply use CakePHP’s Session Component. According to the good folks at CakePHP:
The CakePHP SessionComponent provides a way to persist client data between page requests. It acts as a wrapper for $_SESSION as well as providing convenience methods for several $_SESSION related functions.
The Session component stores the the Auth component. So, if you wanted to know if a user is logged-in to the system, you can do the following:
$user = $this->Session->read('Auth.User') if ($user){ // do something }
One important thing to remember is that you should declare the Session helper in either your helper or controller in order to have access to the Session helper. So don’t forget to do the following.
var $helpers = array('Session');
Thank you you have just saved me 6 hours of frustration.
Thank you sir , you are really a good teacher.