Get module,controller and action ID in Yii 2

This tutorial will show you how to get the module ID,controller ID and action ID.

1.From the Views

Module ID $this->context->module->id
Controller ID $this->context->id
Action ID $this->context->action->id

2.From the controller

 Module ID   Yii::$app->controller->module->id;
 Controller ID   Yii::$app->controller->id
 Action ID  Yii::$app->controller->action->id; or
Module ID       $this->module->id;
Controller ID   $this->id;
Action ID       $this->action->id;

3.in the beforeAction method

Module ID      $action->controller->module->id;
Controller ID   $action->controller->id;
Action ID       $action->id;

Now you know it! Let us know your views so that we help where possible. 🙂

Get Yii RESTful guide manual

Leave a Reply

Your email address will not be published. Required fields are marked *