所以我想要做的是,在我的主要布局中,有一个登录用户的菜单,以及一个不同的匿名用户.
布局将在每个页面上使用,所以我不知道如何做到这一点,正如我所见,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; ?>