Getting Current Controller ID in Yii

2019-01-25 09:43发布

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

5条回答
姐就是有狂的资本
2楼-- · 2019-01-25 10:14

Like this

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

or

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

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

查看更多
放我归山
3楼-- · 2019-01-25 10:18

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.

查看更多
何必那么认真
4楼-- · 2019-01-25 10:19

Yii2:

Yii::$app->controller->id

(Documentation: Application and Controller)

查看更多
孤傲高冷的网名
5楼-- · 2019-01-25 10:29

Controller Id :

$this->id

Here $this refers to controller.

And For getting action id :

$this->action->id
查看更多
forever°为你锁心
6楼-- · 2019-01-25 10:38

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

this will show current controller

查看更多
登录 后发表回答