vinhboy
vinhboy

Reputation: 8982

Accessing the logged in user AuthComponent::user()

http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#accessing-the-logged-in-user

Can someone help me understand this.

The AuthComponent::user() method only returns some basic information about the user, not the actual User object itself.

Coming from rails, I am used to a fully loaded current_user object

If I wanted something similar, should I do a

$this->User->id = AuthComponent::user('id')

and use that? Or is there already a built in method I can use.

Thanks in advance.

Upvotes: 0

Views: 171

Answers (2)

vinhboy
vinhboy

Reputation: 8982

I ended up doing something like this

# load current_user
$this->loadModel('User');
$this->User->id = AuthComponent::user('id');
$user = $this->User->read();
$this->set('current_user',$user);

Upvotes: 0

Dunhamzzz
Dunhamzzz

Reputation: 14798

If you scroll down a bit in the linked documentation you will see that as of CakePHP 2.2 you can now use Containable in your Auth setup, so you could do something like this:

public $components = array(
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'fields' => array('username' => 'email'),
                'contain' => array('RelatedModel', 'RelatedModel')
            )
        )
    )
);

Upvotes: 2

Related Questions