繁体   English   中英

yii中主题的控制器类是什么

[英]what is the controller class for theme in yii

我一直在Yii中创建一个应用程序。 我为管理员创建了一个模块。 因此,我想在主题的左上角显示当前登录用户的角色名称。 我编写了一个方法,该方法返回当前登录用户的角色名称。 我不知道该将哪种方法放置在主题范围内。

ps我在DefaultController.php中编写了该方法,但它仅适用于http:// localhost / [我的项目名称] /index.php/admin/default/index。 当我触发其他控制器时,它给出错误。 在哪里可以写主题方法或任何控制器类主题。

提前致谢

打开protected/components/Controller.php

添加全局变量和函数,然后可以在每个控制器中进行访问。

$public user;

public function init(){
        parent::init(); // no need for this call if you don't have anything in your parent init() 
        getCurrentUser();

}

public function getCurrentUser(){

        $this->user = ... // the code to get user & role name goes here
}

在常用视图中(例如views / layouts / main.php)

<ul>
<li><?php $this->user->user_name ?></li>
<li><?php $this->user->role?></li>
</ul>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM