I have the following code:
Auth->user())
{
echo $this->element(\'header\');
}
else
{
e
Try this
class AppController extends Controller{
$this->user = false;
public function beforeFilter(){
$this->user = $this->Auth->user();
}
public function beforeRender(){
$this->set('logged_user',$this->user);
}
}
Now You can check $logged_user in the view as
if($logged_user){
// users logged in $logged_user have all the details
}else{
// not logged in
}
in cakephp 3 you can check authentication session in the view like this
if($this->request->Session()->read('Auth.User')){
//do when login
}
else{
//do not login
}
//In the views (or layout)
$session->check('Auth.User.id');
//In controller
$this->Auth->User('id');
You don't need to do $this->set(compact('authUser'));
only use this in View:
if ($this->Session->read('Auth.User')){
// do something
}
As of CakePHP 2.x:
<?php if (AuthComponent::user('id')): ?>
Logged in as <?= AuthComponent::user('name') ?>
<?php endif; ?>
This works in Cakephp 3+ (juts modify: "Auth.User.username" to suit your session data)
<?php
if (is_null($this->request->session()->read('Auth.User.username'))) {
echo "....logged out";
} else {
echo "You are Logged in As " . $this->request->session()->read('Auth.User.username');
}
?>