How to enabled and disabled modules in Yii2?

716 Views Asked by At

In google no more information about modules enabled and disabled so I am troubling here.

I have build some modules in yii2 like users, payments, subscriptions, news etc. I want to disabled subscriptions modules. Is it possible?

Edit: Enable and Disable function should allow for end user.

1

There are 1 best solutions below

1
Jap Mul On

I would place the following code in the module class(es) that support being disabled/enabled.

So for every module (users, subscriptions, news, etc) you have module class somewhere that extends \yii\base\Module. Place code like this in every module class and check the appropriate setting.

public function init() {
    if (!$this->_isModuleEnabled()) {
        // This can also be another exception of course.
        throw new \Exception("This module isn't enabled.");
    }
    parent::init();
}

private function _isModuleEnabled() {
    /**
     * Probably check some setting in the database or someting. Then
     * return true or false depending on that setting.
     */
    return true;
}