我是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编写的代码是否为新的Zend_Session_Namespace('sessionUserRole');正确??我确定它不是当我尝试在我的用户控制器中实现它来重定向非管理员用户时,没有重定向操作。我知道这可以通过Zend ACL完成,但我想这样做,这是我学习session_namespace的第一步。
第2部分 如何回应我尝试过的会话变量的值 $ sessionUserRole->的UserRole;?> 但我一无所获。
抱歉,我知道这有很多要问,但我发现Zend网站上的文档几乎不存在,有什么是非常迟钝的,或者也许就是我:-(。
答案 0 :(得分:1)
根据Tim的评论,你的方法getRoles没有那里的用户名 sql query ..现在我们将它作为该方法的参数提供。
如果您想要在没有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;
}