0

我想从 layout.xml 访问会话值。

我做的代码是

布局.xml

<h2><?php  $myprofile=new Zend_Session('user_session');
  print $myprofile->username; ?> </h2>

索引控制器/索引操作

  $userid = $this->_user->getUserId($username,$password);
  $session = new Zend_Session_Namespace('user_session');
  $session->username = $username;
  $session->password = $password;
  $session->uid = $userid;
  $this->_redirect('home');

主页控制器/索引操作

$this->session = new Zend_Session_Namespace('user_session');
$this->view->uname = $this->session->username;

主页/index.phtml

<?php echo "The User Name is ".$this->uname?>

但它显示一个错误

Fatal error: Call to protected Zend_Session::__construct() from context 'Zend_View' in/var/www/shoppingcart/application/layouts/scripts/layout.phtml on line 19

我能够在 Home/index.html 中获取会话值。

期待积极的帮助。

4

2 回答 2

0

我同意 Amine 的观点,将Zend_Session视图脚本放入通常被认为是不好的做法。我确实也会把它放在引导程序中,如下所示......

// Bootstrap.php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    public function _initUserSession()
    {
        // If not already done, bootstrap the view
        $this->bootstrap('view');
        $view = $this->getResource('view');

        // Initialise the session
        $session = new Zend_Session_Namespace('user_session');

        // See if the username has been set, and if not at 
        // least make the variable
        if (isset($session->username)
            $view->username = $session->username;
        else
            $view->username = null;       
    }
}

然后在布局中你可以这样做:

<?php if ($this->username !== null) : ?>
   The User Name is <?php echo $this->username ?>
<?php else : ?>
   No username has been set.
<?php endif; ?>
于 2013-09-24T09:22:21.003 回答
0

为什么在布局中使用 Zend_Session 而在视图中使用 Zend_Session_Namespace?也可能您不应该在视图/布局中使用会话,而是将其作为参数从控制器或引导文件传递

于 2013-09-23T16:29:37.017 回答