【问题标题】:Setting a Session using Nice Auth (CakePHP plugin) and accessing Session information.使用 Nice Auth(CakePHP 插件)设置会话并访问会话信息。
【发布时间】:2012-07-24 23:26:12
【问题描述】:

我在登录时访问会话信息时遇到问题。我正在使用 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');

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

非常感谢任何帮助。

我当前的错误消息如下所示:

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

【问题讨论】:

    标签: php session cakephp authentication


    【解决方案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
    

    【讨论】:

      【解决方案2】:

      在您的控制器中包含会话助手,使用:

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

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

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

      请询问它是否不适合您。

      【讨论】:

        【解决方案3】:

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

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-06-27
          • 1970-01-01
          • 2016-04-17
          • 1970-01-01
          • 1970-01-01
          • 2020-02-09
          相关资源
          最近更新 更多