Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cakephp 2 - check if a user is logged in view

I need a display element according to whether the user is logged or not - In CakePHP 2.0

This does not work

<?php
  if ($this->Auth->loggedIn() 
    { 
    echo $this->element('user');
    }

  else 
    {
    echo $this->element('guest');
    } 
?>

Thanks

like image 397
user1183721 Avatar asked Nov 30 '22 03:11

user1183721


1 Answers

Follow the MVC pattern and put the logic in the controller.

In the controller:

$this->set( 'loggedIn', $this->Auth->loggedIn() );

In the view:

if( $loggedIn ) { 
    echo $this->element( 'user' );
}
else {
    echo $this->element( 'guest' );
} 
like image 165
JJJ Avatar answered Dec 04 '22 09:12

JJJ