加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 从View获取当前用户

发布时间:2020-12-13 22:43:21 所属栏目:PHP教程 来源:网络整理
导读:所以我想要做的是,在我的主要布局中,有一个登录用户的菜单,以及一个不同的匿名用户. 布局将在每个页面上使用,所以我不知道如何做到这一点,正如我所见,Auth组件只能在控制器中使用,如果我只需要在一个控件中执行此操作,这将是很好的查看,但对于每个视图,我该
所以我想要做的是,在我的主要布局中,有一个登录用户的菜单,以及一个不同的匿名用户.

布局将在每个页面上使用,所以我不知道如何做到这一点,正如我所见,Auth组件只能在控制器中使用,如果我只需要在一个控件中执行此操作,这将是很好的查看,但对于每个视图,我该怎么做?我必须在AppController上做点什么吗?

我想做的基本上是

// layout
<?php if(logged): ?>
Welcome <?php echo $user; ?>
<?php else: ?>
Welcom anon,Log in?
<?php endif; ?>
您也可以使用Auth组件访问视图中的登录用户.从 manual:

Once a user is logged in,you will often need some particular
information about the current user. You can access the currently
logged in user using AuthComponent::user(). This method is static,and
can be used globally after the AuthComponent has been loaded. You can
access it both as an instance method or as a static method:

// Use anywhere
AuthComponent::user('id')

// From inside a controller
$this->Auth->user('id');

你应该可以这样做:

// layout
<?php if(AuthComponent::user('name')): ?>
    Welcome <?php echo AuthComponent::user('name'); ?>
<?php else: ?>
    Welcom anon,Log in?
<?php endif; ?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读