How to modify application config settings from within the application

I'm not sure if this is the best way but it can definitely help someone facing the same problems as I did. The scenario is simple, considering that you might use a settings table for your application and want to allow the owner/administrator to change various things such as site name, layout etc from the application itself you may present him with a form which saves the values of those settings so now you need to overwrite whatever is in the main.php configuration file. I added the following code in my bootstrap file (index.php) right after $yiiApp = Yii::createWebApplication ( $config );:

Yii::app ()->name = wb_setting ( 'SITE_NAME' );
Yii::app ()->language = wb_setting ( 'DEFAULT_LANGUAGE' );
Yii::app ()->theme = wb_setting ( 'DEFAULT_THEME' );
Yii::app ()->params->adminEmail = wb_setting ( 'ADMIN_EMAIL' );
Yii::app ()->charset = wb_setting ( 'DEFAULT_CHARSET' );

Where wb_setting is a shortcut function that I use to retrieve the value of a given setting from my db.

Links

Chinese version

Total 1 comment:

#707
App events?
by pestaa at 10:25am on October 7, 2009.

In my opinion it is more vital to preload these values at onBeginRequest event of applications.

Your Comment:

You may enter comment using Markdown syntax.

Please login with your forum account.
Note: you must have at least ONE forum post with your account.