How to modify application config settings from within the application

2 followers

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 version1

Total 2 comments

#2259 report it
trejder at 2010/12/03 06:32am
Config extension

For storing app-specific settings in DB, config Extension seems to be a better solution than proposed in this article. Mainly because it introduces use of caching component, which is a vital part of app, when storing settings in DB rather than in a file!

#1271 report it
pestaa at 2009/10/07 02:25am
App events?

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

Leave a comment

Please to leave your comment.

Write new article
  • Written by: manilodisan
  • Updated by: milkyway
  • Category: Tutorials
  • Votes: -5
  • Viewed: 3,100 times
  • Created on: Oct 3, 2009
  • Last updated: Jun 16, 2011