CakePHP check if user is logged in inside a view

Cameron picture Cameron · May 5, 2011 · Viewed 65.8k times · Source

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

Answer

meotimdihia picture meotimdihia · Jun 27, 2011

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

if ($this->Session->read('Auth.User')){
// do something 
}