Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP check if user is logged in inside a view

Tags:

php

cakephp

I have the following code:

    <?php      if (!$this->Auth->user())     {         echo $this->element('header');     }     else     {         echo $this->element('header-bar');     }      ?> 

inside my view which should show a different header for logged in users but throws the following error:

Notice (8): Undefined property: View::$Auth [APP/views/layouts/page.ctp, line 17] Fatal error: Call to a member function user() on a non-object in /Users/cameron/Sites/thehive/app/views/layouts/page.ctp on line 17 

How do I fix this? Thanks

like image 757
Cameron Avatar asked May 05 '11 17:05

Cameron


1 Answers

You don't need to do $this->set(compact('authUser')); only use this in View:

if ($this->Session->read('Auth.User')){ // do something  } 
like image 95
meotimdihia Avatar answered Oct 16 '22 05:10

meotimdihia