June 17, 2015

In this article, we are going to discuss about How to load the modules dynamically from DB or from Directory in YII framework. In Yii application development, we can set up modules configuration in config.php.  Sometimes, we need to load modules configuration from database or directory glob. After searching the topic in google and comparing some solutions, I found the elegant way to implement this. Hope it can help.

You can modify the index.php file of your Yii application.

require_once($yii);

class ExtendableWebApp extends CWebApplication {
        protected function init() {
                // this example dynamically loads every module which can be found
                // under `modules` directory
                // this can be easily done to load modules
                // based on MySQL db or any other as well
                foreach (glob(dirname(__FILE__).'/protected/modules/*', GLOB_ONLYDIR) as $moduleDirectory) {
                        $this->setModules(array(basename($moduleDirectory)));
                }
                return parent::init();
        }
}

$app=new ExtendableWebApp($config);
$app->run();

Reference: http://www.yiiframework.com/forum/index.php/topic/23467-dynamically-load-modules-models-and-configurations/page__p__144316#entry144316

0 comments:

Post a Comment