我想从layout.xml访问会话值。
我做的代码是
Layout.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;
Home/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中获得会话值。
期待积极的帮助。
发布于 2013-09-24 00:29:37
为什么在布局中使用Zend_Session,在视图中使用Zend_Session_Namespace?此外,您可能不应该在视图/布局中使用会话,而应将其作为参数从控制器或引导程序文件传递
发布于 2013-09-24 17:22:21
我同意我的观点,将Zend_Session
放在视图脚本中通常被认为是不好的做法。我确实也会把它放在bootstrap中,如下所示……
// 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; ?>
https://stackoverflow.com/questions/18963815
复制相似问题