Search code examples
phpyii2yii2-advanced-appyii2-modelyii2-module

Yii2 access to module instance from model


I want to know if there is a way to access at the module instance from a model. In my module configuration I have some components which I need to call in a model class, the model is defined only inside my module. I know the method Yii::$app->getModule('my-module') but this mean that the module have to be defined every time with the same name, from controller's module I can access as $this->module->component-name, something similar in the model?


Solution

  • Yes you can access the module instance like this:

    $module = MyModuleClass::getInstance();
    

    Then you can get id via

    $module->id
    

    For more information check here