php – 从View获取当前用户

所以我想要做的是,在我的主要布局中,有一个登录用户菜单,以及一个不同的匿名用户.

布局将在每个页面上使用,所以我不知道如何做到这一点,正如我所见,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; ?>

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...