Getting Current Controller ID in Yii
Asked Answered
C

5

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

Cicely answered 6/3, 2012 at 10:55 Comment(0)
T
7

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

this will show current controller

Timeworn answered 6/3, 2012 at 20:37 Comment(0)
D
25

Like this

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

or

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

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

Drainage answered 6/3, 2012 at 11:38 Comment(1)
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.Unmixed
P
13

Controller Id :

$this->id

Here $this refers to controller.

And For getting action id :

$this->action->id
Pricecutting answered 6/3, 2012 at 10:59 Comment(1)
thanks for help but. I will look for the current controller in my main.php in my layout files... is that possible?Cicely
T
7

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

this will show current controller

Timeworn answered 6/3, 2012 at 20:37 Comment(0)
A
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.

Assam answered 1/10, 2012 at 13:18 Comment(0)
R
1

Yii2:

Yii::$app->controller->id

(Documentation: Application and Controller)

Rhine answered 26/9, 2017 at 15:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.