Yii Framework Forum: Accessing constants in config/main file - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Accessing constants in config/main file Rate Topic: -----

#1 User is offline   ps_sach 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 79
  • Joined: 07-December 09

Posted 17 March 2010 - 02:32 AM

Hi all,

I want to access a veriable from constant component file in the config/main.php file. For example,
I have a ConfigManager Class in my component dir as

<?php
class ConfigManager {
        public static $dbUserName = 'root';
        public static $dbPassword = 'test123';
}
?>



And in config file I want to do it like this -

'db'=>array(
        'class'=>'CDbConnection',
        'connectionString'=>'mysql:host=localhost;dbname=dbname',
        'username'=> ConfigManager::$dbUserName,
        'password'=> ConfigManager::$dbPassword,
        'emulatePrepare'=>true,
),



But it is giving require error. Can't find class ConfigManager. Is it possible to do it like this?

Thanks!
0

#2 User is offline   jsoo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 65
  • Joined: 01-March 10
  • Location:Durham, NC, USA

Posted 17 March 2010 - 06:14 AM

The components aren't loaded until after the config file. Not surprising, given that the config file contains the information needed to load them. You could include/require ConfigManager.php in your config file.
0

#3 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 17 March 2010 - 06:42 AM

Let me ask one question: What do you win by using such a ConfigManager class?
0

#4 User is offline   ps_sach 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 79
  • Joined: 07-December 09

Posted 18 March 2010 - 02:44 AM

View PostMike, on 17 March 2010 - 06:42 AM, said:

Let me ask one question: What do you win by using such a ConfigManager class?



Hi Mike,
Actually I have some other configuration parameters like

     $serverTimeZone = 'Asia/Calcutta';




What I would like to have single configuration file that just need to be edited while configuring the application. So I want to have ConfigManager file to place all the configurations and it will configure rest of the application.

May be its not right idea!

Please replay!

Thanks again.
0

#5 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 18 March 2010 - 04:10 AM

There are lot of ways to do something similar. My favorite approach is to keep a file local.php in config/ that gets merged into the main.php config array. That way you can override/add settings in your local config file. Similar to this:

http://www.yiiframew...oc/cookbook/32/
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

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