[英]How do I print the username using session in cakephp?
当我从会话中调用它时,我无法在网站上显示我的用户名。
索引页面上的代码是:
<h1>Users Home</h1>
Welcome <?php $user = $this->Session->read('Users.username');?>
我究竟做错了什么?
我还尝试了各种其他方法来调用它,然后获取不同的错误消息。
在您的代码中,您使用用户名的内容设置$ user。 但你不打印它。
<h1>Users Home</h1>
Welcome <?=$this->Session->read('Auth.User.username')?>
这是短的
<h1>Users Home</h1>
Welcome <?php print $this->Session->read('Auth.User.username'); ?>
正如雨果所说,你应该做到以下几点:
<h1>Users Home</h1>
Welcome <?php echo $this->Session->read('Auth.User.username'); ?>
如果您打算在多个视图上使用它,我建议您在AppController上添加以下内容。
public function beforeFilter() {
$this->set('username', AuthComponent::user('username'));
// OR $this->set('username', $this->Session->read('Auth.User.username'));
}
然后在您的任何视图上使用变量$ username来打印当前用户名。
<?php echo $username; ?>
echo $this->Session->read('Auth.User.username');
或者,如果您使用的是Cakephp 2.x,则可以在代码中的任何位置使用AuthComponent::user('username')
。
获得$ user后,你应该使用echo $ user或print_r($ user),你将获得打印的用户名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.