13

I have module ticket.

Module class has propery UserClassName (string).

In this module I have model called Dialog.

Within this model, I want to get access to the module property UserClassName.

How I can get module object from my model Dialog?

P.S. From controllers I can do next: $this->module.

LostDok
  • 155
  • 1
  • 2
  • 10

4 Answers4

34

You can use className() to obtain the class name.

$yourModule = yourPath\YourModule::className();   

Or for getting the module id you can use:

Yii::$app->controller->module->id; 
arogachev
  • 33,150
  • 7
  • 114
  • 117
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
4

we can identify the module,controller and action method as follows in yii2
Yii::$app->controller->module->id;(For Getting Module ID)
Yii::$app->controller->id;(For Getting Controller ID)
Yii::$app->controller->action->id;(For Getting Controller action ID)

Rafael
  • 1,495
  • 1
  • 14
  • 25
0

I found a solution (I need same for me too). To get module object - very simple:

\Yii::$app->getModule('moduleName')->propertyOrMethod;
Verter
  • 181
  • 2
  • 11
0
$module = MyModuleClass::getInstance();

The getInstance() method will return the currently requested instance of the module class. If the module is not requested, the method will return null. Note that you do not want to manually create a new instance of the module class because it will be different from the one created by Yii in response to a request.

Oleg
  • 7,070
  • 4
  • 47
  • 49