I am having issues accessing session information upon login. I am using Nice Auth a plugin for CakePHP.
I have followed some instructions here
What I am trying to do is access the users username so that on another page they do not need to fill out who they are when they send a support ticket.
This is the Userscontroller login function:
public function login(){
if ($this->request->is('post') || ($this->request->is('get') && isset($this->request->query['openid_mode']))) {
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
}
else {
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
}
Which is where I would assume I put the
$this->Session->write('User.id', $userId);
Below is the tickets controller:
public function send()
{
$userId = $this->Auth->user();
if ( !empty($this->request->data) )
{
$email = new CakeEmail();
$email->from(array('xxxx@example.com'))
//->to($this->request->data['to'])
->to(array('helpdesk@example.com'))
->subject($this->request->data['subject']);
if ($email->send($this->request->data['message']))
{
$this->Session->setFlash(__('Email Sent Successfully'),
'default', array('class' => 'success'));
}
}
}
Where I would expect to put this code:
$userId = $this->Session->read('User.id');
And then to display in my View:
$userId = $session->read('User.id');
Now, I have added the Session Component and Helpers to the files as I thought that would be what was causing me issues, but No dice!
Any help is greatly appreciated.
My Current error messages look like this:
Notice (8): Undefined variable: session [APP/View/Tickets/send.ctp, line 10] Fatal error: Call to a member function read() on a non-object in /Users/bellis/workspace/cake/app/View/Tickets/send.ctp on line 10
Turns out, the recommended method is to do it like this:
You can access the logged in user via that method from any controller.