使用Zend_Session_Namespace创建会话和访问变量

问题描述 投票:4回答:1

我是Zend Framework的新手,我有一个关于使用Zend_Session_Namespace创建会话和访问变量的问题。

我想我的问题有两个部分,首先是我正确创建会话,其次我如何回应变量来测试我是否正确地进行了操作。

第1部分从我所看到的,这就是我所做的。

在我的bootstrap文件中,我创建了以下函数

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

在我的登录控制器中,这是基于我从Zend Framework获得的代码的初学者指南,我为上面创建了一个新的命名空间。

// 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网站上的文档几乎不存在,有什么是非常迟钝的,或者也许就是我:-(。

php zend-framework
1个回答
1
投票

根据Tims的评论,你的方法getRoles没有用于sql查询的用户名。现在我们将它作为方法的param提供。

如果你想在没有Zend的情况下访问Session,只需使用: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;
    }
© www.soinside.com 2019 - 2024. All rights reserved.