Module : install and dynamic load

Hi,

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.

Thanks

8)

(ok ok I reply to my own post ;D)

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();


?>

(and it works fine)

8)

Another way is modifying your config file like this:



array(


    ...


    'modules'=>require(dirname(__FILE__).'/modules.php',


    ....


);


in modules php do something like this:



return searchForModules();





function searchForModules(){


$arr = array();


/* add your modules dynamically here */


return $arr;


}


Or just add a funcition befor

hi sebas,

thanks for your answer … and good idea.

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 !

8)