Yii2 Get current action in controller
You can get current action name by:
Yii::$app->controller->action->id
And get the controller name with this one:
Yii::$app->controller->id;
Note: Remember that these will only work after the application has been initialized. Possible use: inside a controller action
/ inside a model
or inside a view
Reference: Class yii\web\Controller
You should use beforeAction() event instead of init()
.
Also you can simply use $this
because it contains current controller.
public function beforeAction($action)
{
if (parent::beforeAction($action)) {
if ($this->action->id == 'lang') {
Url::remember();
}
return true; // or false if needed
} else {
return false;
}
}
if use Yii2 in view - try this:
$this->context->action->id;