Zend session

From EKiniWiki

Jump to: navigation, search

This example shows how to add variables in the Zend_Auth Session Namespace.

require_once 'Zend/Session/Namespace.php';
...
public function loginAction()
{
...
use zend_auth to login here...
...
$dbAdapter = Zend_Registry::get('dbAdapter');
$sql = 'SELECT
    ur.role_id,
    r.role_name
    FROM users_to_roles ur
    LEFT JOIN roles r ON ur.role_id = r.id
    WHERE ur.user_id="'.$user->id.'"';
    $roles = $dbAdapter->fetchAll($sql);
                        
    $myroles = '';
    foreach ($roles AS $role) {
        //$myroles .= $role['role_name'].'|';
        $myroles[] = $role['role_name'];
    }
                        
    //get the Zend_Auth and then save the roles
    $authNamespace = new Zend_Session_Namespace('Zend_Auth');
    $authNamespace->storage->myroles = $myroles;
                        
...
}

If I dump the Zend_Auth Session Namespace, I will get this:

object(stdClass)#21 (9) {
  ["id"] => string(1) "1"
  ["username"] => string(7) "wenbert"
  ["first_name"] => string(7) "Wenbert"
  ["last_name"] => string(11) "Del Rosario"
  ["email"] => string(17) "wenbertttttt@ekini.net"
  ["registered_date"] => string(19) "2007-07-21 14:06:12"
  ["last_login"] => NULL
  ["website"] => string(20) "http://www.ekini.net"
  ["myroles"] => array(1) {
    [0] => string(5) "admin"
  }
}

Then in my view file (.phtml), I could do something like this:

...
    <h2><?=$this->song_title?></h2>
    <?php
    if (isset($this->user)) {
        if (in_array('moderator',$this->user->myroles) OR in_array('admin',$this->user->myroles)) {
            echo '<div class="mod_options">';
            echo '<a href="'.$this->baseUrl.'/index/editlyrics/artist/'.$this->artist.'/song/'.$this->song.'">Edit This</a>';
            echo '</div>';
        }
    }
    ?>
...
Personal tools
Bookmarks