You can call this ‘dirty’, but what I used to do with something like this was to include the code in my /layouts/main.php, because in my case, I only use one layout for the whole application, thus only one main.php layout is being called. I did this when I was starting to learn Yii, and I was echo-ing and print_r-ing and var_dump-ing Yii::app()->controller->id,Yii::app()->user->name, etc., just for the sake of experimenting. I just remembered it now, since it can give what you want to know about almost everything in action in every page load.
Another option is to put afterAction() or beforeAction in your application’s main controller (/protected/components/Controller.php by default), and get the controller by $this->getId(), or action by $this->getAction()->getId(). I think this is much neater. You are still welcome to use my first suggestion though (nobody will notice )
/**
* Controller is the customized base controller class.
* All controller classes for this application should extend from this base class.
*/
class Controller extends Controller
{
/**
* @var string the default layout for the controller view. Defaults to '//layouts/column1',
* meaning using a single column layout. See 'protected/views/layouts/column1.php'.
*/
public $layout='//layouts/column2';
/**
* @var array context menu items. This property will be assigned to {@link CMenu::items}.
*/
public $menu=array();
/**
* @var array the breadcrumbs of the current page. The value of this property will
* be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
* for more details on how to specify this property.
*/
public $breadcrumbs=array();
public function init() {
parent::init();
$abc = array('controller' => 'init');
var_dump($abc);
}
}
Uhm, not component/Controller.php should extend from Controller - but your controllers in the /proctected/controllers/ dir!
BTW:
Didn’t you notice that it’s hard to extend one class from the same class?? (Controller extends Controller). No offense, but usually it doesn’t hurt to think, before writing some code …