StreetCoder
StreetCoder

Reputation: 10061

what is the controller class for theme in yii

I have been creating an application in Yii. I created a module for admin. So, I want to show the current logged in user's role name in the top-left of my theme. I have written a method which returns the name of the role of current logged in user. I do not understand where I should place this method which will work in theme.

p.s. I wrote that method in DefaultController.php but it works only for http://localhost/[my project name]/index.php/admin/default/index. When I trigger to other controller it gives error. Is there any place where I write methods for theme or any controller class for theme.

Thanks in advance

Upvotes: 0

Views: 77

Answers (1)

Telvin Nguyen
Telvin Nguyen

Reputation: 3559

Open protected/components/Controller.php

Add global variable & function then you can access in every controller.

$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
}

In common view (such as views/layouts/main.php)

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

Upvotes: 0

Related Questions