登录时访问会话信息时遇到问题。我正在使用Nice Auth作为CakePHP的插件。
我已按照一些说明here
我要做的是访问用户的用户名,以便在另一个页面上,他们不需要填写他们发送支持服务单时的身份。
这是Userscontroller登录功能:
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'));
}
}
}
我认为我把
放在哪里$this->Session->write('User.id', $userId);
以下是门票控制器:
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'));
}
}
}
我希望将此代码放在哪里:
$userId = $this->Session->read('User.id');
然后在我的视图中显示:
$userId = $session->read('User.id');
现在,我已经将Session Component和Helpers添加到文件中,因为我认为这会导致我的问题,但是没有骰子!
非常感谢任何帮助。
我的当前错误消息如下所示:
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
答案 0 :(得分:1)
set session in your $helpers array in your controller or app controller
var $helpers=array('Session');
if you have use cake 1.2 ver use above code and if you have use higher ver like 2.0, 2.1 etc use below code
public $helpers = array('Session');
your can set variable in controller
$userId = $this->Session->read('User.id');
$this->set('userid', userId );
and directly access $userid variable in your ctp file
OR
You can directly access
$userId = $this->Session->read('User.id');
in your ctp file
答案 1 :(得分:0)
使用以下命令在控制器中包含会话助手:
public $helpers = array('Session',.........);
并尝试在您的视图中使用以下内容:
$userId = $this->Session->read('User.id');
请问它是否对您不起作用。
答案 2 :(得分:0)
事实证明,推荐的方法是这样做:
$userId = $this->Auth->user('id');
您可以从任何控制器通过该方法访问登录用户。