CakePHP 3.x-视图中的AuthComponent :: user() [英] CakePHP 3.x - AuthComponent::user() in View

查看:73
本文介绍了CakePHP 3.x-视图中的AuthComponent :: user()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在CakePHP 2.x中,您可以做到

In CakePHP 2.x you could do

 AuthComponent::user()

在视图中从Auth组件获取数据。在CakePHP 3.0beta3中,它引发:

in View to get data from Auth component. In CakePHP 3.0beta3 it throws:

 "Error: Class 'AuthComponent' not found"

是否有一种简单的方法可从View的AuthComponent获取数据?

Is there a simple way to get data from AuthComponent in View?

推荐答案

蛋糕3.5

AppController 中:

public function beforeRender(Event $event) {
    ....

    $this->set('Auth', $this->Auth);
}

.ctp 模板中:

<?php if (!$Auth->user()) { ?>
    <a class="login" href="<?php echo $this->Url->build($Auth->getConfig('loginAction')); ?>">Login</a>
<?php } else { ?>
    <div class="name"><?php echo h($Auth->user('name')); ?></div>
<?php } ?>

这篇关于CakePHP 3.x-视图中的AuthComponent :: user()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆