2011-04-09 65 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