开发者

Creating sessions and accessing the variables using Zend_Session_Namespace

I'm new to the Zend Framework and I have a question regarding creating sessions and accessing the variables using Zend_Session_Namespace.

I guess my question has two parts, first am I creating the session correctly and secondly how can I echo the variable to test if I am doing it correctly.

Part 1 From what I've read this is what I've done.

In my bootstrap file I've created the following function

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

Within my login controller which is based on code I've gotten from Zend Framework a beginners guide I've created a new namespace for the above.

// 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;
  }

Is the code I've written for $sessionUserRole = new Zend_Session_Namespace('sessionUserRole'); correct?? I'm sure its not as when I try to implement it in my users controller to redirect non admin users there is no redirection actioned. I know this can be done through Zend ACL but I want to do it this way and its my first step in learning session_namespace.

Part 2 How can I echo the value of a session variable I've tried $sessionUserRole->userRole;?> But Im getting nothing.

Sorry I know this is a lot to be asking but I've found that the documentation on this is nearly non existent on the Zend site and what is there is very obtuse, or maybe thats just me :-( .


Based on Tims comment, your method getRoles does not have the username for there sql query.. now we provide it as param to the method.

if you want to access the Session without Zend, just use: 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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜