Yii Framework Forum: Application global preferences - best practice - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Application global preferences - best practice Rate Topic: -----

#1 User is offline   KJedi 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 381
  • Joined: 19-October 08
  • Location:Nikolaev, Ukraine (Europe)

Posted 02 December 2008 - 06:00 AM

What is best approach to store the application configs, that should be edited by users via web-interface? That's not user-related, just global variables like caching period, number of items in lists etc.
0

#2 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,902
  • Joined: 04-October 08
  • Location:DC, USA

Posted 02 December 2008 - 07:53 AM

CApplication has "params" property which can be used to store global parameters.
In your app config, you can do the following:

return array(
    ............
    'params'=>include('path/to/params.php');
);


Then you can put all your parameters in a separate file in params.php like the following:

return array(
    'cachingDuration'=>10000,
    'pageSize'=>10,
    .....
);


In order to read a parameter, you use Yii::app()->params['pageSize'].
0

#3 User is offline   KJedi 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 381
  • Joined: 19-October 08
  • Location:Nikolaev, Ukraine (Europe)

Posted 02 December 2008 - 07:56 AM

thanks, but I want admin to be able to change that... is there any clever way to load params from the DB? For exmple, in he params.php I'll query DB for global params and return them. Will that work either?
0

#4 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,902
  • Joined: 04-October 08
  • Location:DC, USA

Posted 02 December 2008 - 08:01 AM

Yes, that will work, but not very elegant, because you put complex logic inside a config file. The query also gets executed for every page, unless you use some caching.

An alternative way is to write a static class like UserPreference. You use this class to read and write parameters, instead of relying on app's params.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users