php – 使用Zend_Session_Namespace创建会话和访问变量
我是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部分 对不起,我知道这有很多要问,但我发现Zend网站上的文档几乎不存在,有什么是非常迟钝的,或者也许就是我:-(. 解决方法
根据Tim的评论,你的方法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; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |