【问题标题】:Creating sessions and accessing the variables using Zend_Session_Namespace使用 Zend_Session_Namespace 创建会话和访问变量
【发布时间】:2011-04-09 14:26:37
【问题描述】:

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

我想我的问题有两个部分,首先我是否正确创建会话,其次如何回显变量以测试我是否正确执行。

第 1 部分 根据我的阅读,这就是我所做的。

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

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 网站上几乎不存在这方面的文档,而且那里的内容非常迟钝,或者可能就是我自己 :-( 。

【问题讨论】:

  • 看起来“不错”..你的 $_SESSION 全局数组里面有什么?
  • getRole方法中的$values从何而来?我建议在你的 foreach 循环中添加一些调试代码,看看是否有任何角色被添加到会话中。
  • @ArneRie 我是个新手,我不知道如何在 Zend 中访问它抱歉
  • @Tim Fountain $values 来自我的登录表单,因此我确信它们至少在某些时候具有值,因为它们允许我登录。但我会看看
  • $values 来自登录位中的表单,但 getRole 函数有自己的范围,并且 $values 永远不会设置(除非它在您未发布的部分代码中完成) .

标签: php zend-framework


【解决方案1】:

根据 Tims 的评论,您的方法 getRoles 没有那里的用户名 sql 查询.. 现在我们将它作为参数提供给方法。

如果你想在没有 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;
    }

【讨论】:

  • 是的,你的答案是正确的,我看到了我在 Tims cmets 中犯的错误,并通过添加参数对其进行了排序。这对我来说简直是愚蠢的错误。我可能是 Zend 的新手,但即使是我也应该发现这一点。
猜你喜欢
  • 1970-01-01
  • 2015-07-21
  • 2011-10-23
  • 1970-01-01
  • 2019-05-04
  • 1970-01-01
  • 1970-01-01
  • 2011-12-29
  • 1970-01-01
相关资源
最近更新 更多