CakePHP 3.x – View中的AuthComponent :: user()

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了CakePHP 3.x – View中的AuthComponent :: user()脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
在Cake PHP 2.x中你可以做到
Authcomponent::user()

在View中从Auth组件获取数据.在CakePHP 3.0beta3中,它抛出:

"Error: Class 'AuthComponent' not found"

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

蛋糕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 – View中的AuthComponent :: user()全部内容,希望文章能够帮你解决CakePHP 3.x – View中的AuthComponent :: user()所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。