Yii Framework 2. Get the current controller name, action, module

April 25, 2016 68 egor

A simple example of code for Yii2, how get the name of the current controller, action or module.

Call from view:

<?php
echo Yii::$app->controller->id; //the name of the current controller
//Yii::$app->controller get a controller as an object
echo Yii::$app->controller->action->id; //name of the current action
echo Yii::$app->controller->module->id; //the name of the current module

Call from controller:

<?php
echo $this->id; //the name of the current controller
echo $this->action->id; //name of the current action
echo $this->module->id; //the name of the current module