2012-07-24 30 views
0

我在登錄時訪問會話信息時遇到問題。我正在爲CakePHP使用Nice Auth插件。使用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('[email protected]')) 
       //->to($this->request->data['to']) 
       ->to(array('[email protected]')) 
       ->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 

回答

0

事實證明,推薦的方法是做這樣的:

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

您可以通過該方法從任何控制器訪問用戶登錄。

0

使用包含會話助手在你的控制器:

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

,並嘗試使用您的看法如下:

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

請問如果它不適合你。

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