my webapp should be able to integrates modules, and theses modules should be installed through a simple procedure that would not imply to modify directly the app configuration file (I'm currently working on that).
Assuming a module has been correctly installed, a row is added in the module table.
Now, the question is : how is it possible to load a module and make it available to the webapp, without modifying the config file ? … in other words, how to dinamically load modules ?
My first idea is to extend CWebApplication and call setModules() on init() … or somewhere else. Maybe an event handler attached to the CWebApplication would be more clean ? … really, I don’t know, and hopefully someone will be able to help.
So, this is what I did to dynamically load modules … is it ok ? … but please, if anyone has a better approach, let me know.
This is the webapp entry script (webapp/index.php):
<?php
// remove the following line when in production mode
defined('YII_DEBUG') or define('YII_DEBUG',true);
require_once($yii);
class ExtendedWebApplication extends CWebApplication {
public function __construct($config=null)
{
parent::__construct($config);
$this->_loadModules();
}
private function _loadModules()
{
$criteria=new CDbCriteria;
$criteria->select='id, name';
$criteria->condition='is_enabled=:enabled';
$criteria->params=array(':enabled' => 1);
$modules=Module::model()->findAll($criteria);
if( count($modules)!=0)
{
$this->setModules(array_values(CHtml::listData($modules,'id','name')));
}
}
}
$exWebApp=new ExtendedWebApplication($config);
$exWebApp->run();
?>
Acutally I think it also possible to overload CWebApplication:: beforeControllerAction() and load modules here. I didn’t try that (just read it in the api doc) but maybe that’s better than overloading the contructor… I don’t know what a best-practise would be … Yii provides sooo many ways to reach the same goal
[edit] … no, forget it ! when beforeControllerAction is called, the controller is already loaded … too late to load modules !