使用Nice Auth(CakePHP插件)设置会话并访问会话信息


Setting a Session using Nice Auth (CakePHP plugin) and accessing Session information.

登录时访问会话信息时遇到问题。我正在使用Nice Auth作为CakePHP的插件。

我在这里遵循了一些说明

我想做的是访问用户的用户名,这样在另一个页面上,当他们发送支持票证时,就不需要填写他们是谁了。

这是用户控制器登录功能:

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');

现在,我已经将会话组件和帮助程序添加到文件中,因为我认为这会导致我出现问题,但没有骰子!

非常感谢您的帮助。

我当前的错误消息如下:

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
    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

使用在控制器中包含会话助手

 public $helpers = array('Session',.........);

并尝试在您的视图中使用以下内容:

$userId = $this->Session->read('User.id');

请询问它是否对您无效。

事实证明,推荐的方法是这样做:

$userId = $this->Auth->user('id');

您可以通过该方法从任何控制器访问登录的用户。