2011-04-09 59 views
3

我是Zend Framework的新手,我有一個關於創建會話和使用Zend_Session_Namespace訪問變量的問題。Zend_Session_Namespace需要幫助

我想我的問題有兩個部分,一是我正確創建會話,二是我如何回顯變量以測試我是否正確執行該操作。

第1部分 從我讀過的這是我所做的。

在我的引導文件,我創建了下面的函數

protected function _initSession() 
    { 
     Zend_Session::start(); 
     $sessionUserRole = new Zend_Session_Namespace('sessionUserRole'); 
    } 

在我的登錄控制器是基於代碼中,我從Zend框架得到一個新手引導我創建了一個新的命名空間,以上。

// login action 
    public function loginAction() 
    { 

    $form = new PetManager_Form_Login; 
    $this->view->form = $form;  

    /* 
    check for valid input from the form and authenticate using adapter 
     Add user record to session and redirect to the original request URL if present 
*/ 
    if ($this->getRequest()->isPost()) { 
    if ($form->isValid($this->getRequest()->getPost())) { 
     $values = $form->getValues(); 

    $adapter = new PetManager_Auth_Adapter_Doctrine(
     $values['username'], $values['password'] 
    ); 
     $auth = Zend_Auth::getInstance(); 
     $result = $auth->authenticate($adapter); 

    if ($result->isValid()) { 
     $session = new Zend_Session_Namespace('petmanager.auth'); 
     $session->user = $adapter->getResultArray('username','Password'); 
     // $sessionUserRole not Working ?????????   
    $sessionUserRole = new Zend_Session_Namespace('sessionUserRole'); 
    foreach($this->getRole() as $r) 
      { 
      $sessionUserRole->userRole = $r['userType']; 
    } 

     if (isset($session->requestURL)) { 
     $url = $session->requestURL; 
     unset($session->requestURL); 
     $this->_redirect($url); 
     } else { 
     $this->_helper->getHelper('FlashMessenger') 
         ->addMessage('Welcome '.$auth->getIdentity().'. You have been successfully logged in.'); 
     $this->_redirect('/login/success'); 
     } 
    } else { 
     $this->view->message = 'You could not be logged in. Please try again.';   
    }   
    } 
    } 
    } 

public function getRole() 
    { 
    $q = Doctrine_Query::create() 
     ->select('u.userType') 
      ->from('PetManager_Model_Users u') 
      ->where('u.name = ?', $values['username']); 
    $result = $q->fetchArray(); 
return $result; 
    } 

是我寫的代碼$ sessionUserRole = new Zend_Session_Namespace('sessionUserRole');正確??我確信它不是當我嘗試在我的用戶控制器中實現它來重定向非管理員用戶時,沒有重定向動作。我知道這可以通過Zend ACL完成,但我想這樣做,這是我學習session_namespace的第一步。

2部分 我如何可以重複會話變量的值,我試過 $ sessionUserRole-> UserRole的;?> 但是我卻一無所獲。

對不起,我知道這是很多要問,但我發現這個文檔在Zend網站上幾乎是不存在的,什麼是非常鈍的,或許這就是我:-(。

提前對所有的答覆。非常感謝

格雷厄姆

+0

看起來「OK」 ..什麼是您的$ _SESSION全局數組裏面? – opHASnoNAME 2011-04-09 18:06:00

+0

$ value在getRole方法中來自哪裏?我建議在foreach循環中添加一些調試代碼,以查看是否有任何角色被添加到會話中。 – 2011-04-09 18:35:50

+0

@ArneRie我是一個新手,我不知道如何在Zend訪問這個對不起 – Graham 2011-04-09 18:39:34

回答

1

基於蒂姆斯評論,你的方法getRoles沒有用戶名有 SQL查詢。現在我們將它作爲參數去的方法。

如果您要訪問不Zend的會話,只需使用:var_dump($_SESSION);

// login action 
    public function loginAction() 
    { 
     $form = new PetManager_Form_Login(); 
     $this->view->form = $form; 

     if ($this->getRequest()->isPost()) { 
      if ($form->isValid($this->getRequest()->getPost())) { 
       $values = $form->getValues(); 
       $adapter = new PetManager_Auth_Adapter_Doctrine(
        $values['username'], $values['password'] 
       ); 
       $auth = Zend_Auth::getInstance(); 
       $result = $auth->authenticate($adapter); 

       if ($result->isValid()) { 
        $session = new Zend_Session_Namespace('petmanager.auth'); 
        $session->user = $adapter->getResultArray('username','Password'); 
        $sessionUserRole = new Zend_Session_Namespace('sessionUserRole'); 

        // username from form 
        foreach ($this->getRole($values['username']) as $r) { 
         $sessionUserRole->userRole = $r['userType']; 
        } 

        if (isset($session->requestURL)) { 
         $url = $session->requestURL; 
         unset($session->requestURL); 
         $this->_redirect($url); 
        } else { 
         $this->_helper->getHelper('FlashMessenger')->addMessage(
          'Welcome ' . $auth->getIdentity() . 
          '. You have been successfully logged in.' 
         ); 
         $this->_redirect('/login/success'); 
        } 
       } else { 
        $this->view->message = 'You could not be logged in. Please try again.'; 
       } 
      } 
     } 
    } 

    /** 
    * Get role for an user 
    * 
    * @param string $username User 
    * 
    * @return array roles 
    */ 
    public function getRole($username) 
    { 
     $q = Doctrine_Query::create()->select('u.userType') 
      ->from('PetManager_Model_Users u') 
      ->where('u.name = ?', $username); 

     $result = $q->fetchArray(); 
     return $result; 
    } 
+0

是的,你的回答是正確的我看到我從蒂姆斯的評論中犯的錯誤,並通過添加參數對其進行了排序。這代表了我的愚蠢錯誤。我也許是Zend的新手,但即使我應該看到這一點。 – Graham 2011-04-10 11:03:49