10

I want to get the current controller name that handles the current action. but the in my case I will look for the current controller in my main.php in my layout files.

this is my small view of my directory structure to give you an idea where is my layout files and the file where i will put my codes in searching of my controller name

  • /protected
  • /themes
    • /mylayout
      • /layouts
        • main.php
        • column1.php
        • column2.php
      • /site
        • index.php

Is this possible? im trying the following codes but i failed to get my current controller name...

echo Yii::app()->controller->getId;
echo Yii:app()->getController->id;
echo Yii:app()->controller->uniqueID;

thanks

tereško
  • 58,060
  • 25
  • 98
  • 150
Netorica
  • 18,523
  • 17
  • 73
  • 108

5 Answers5

25

Like this

Yii::app()->controller->id

or

Yii::app()->getController()->getId()

http://www.yiiframework.com/doc/api/1.1/CApplication#getController-detail

schmunk
  • 4,708
  • 1
  • 27
  • 50
  • 2
    I prefer your answer, because your answer works any where, views, widgets, extensions, etc. E.G. in case of calling CGridView, $this refers to it's widgets and $this->id results in widget name and not controller name. – hpaknia Apr 21 '13 at 11:19
13

Controller Id :

$this->id

Here $this refers to controller.

And For getting action id :

$this->action->id
Onkar Janwa
  • 3,892
  • 3
  • 31
  • 47
  • thanks for help but. I will look for the current controller in my main.php in my layout files... is that possible? – Netorica Mar 06 '12 at 11:03
7

<?php echo $this->getUniqueId();?>

this will show current controller

22francis
  • 509
  • 4
  • 14
3

You're not actually required to use the static function. Whenever in a view( or template) you can use echo $this->getUniqueId(); to get the unique controller ID.

Raz0rwire
  • 686
  • 9
  • 18
1

Yii2:

Yii::$app->controller->id

(Documentation: Application and Controller)

TheStoryCoder
  • 3,403
  • 6
  • 34
  • 64