Global Variables in Yii

How can I use global variables in Yii?

Actually I am integrating PhpBB with Yii application. The class for integration works fine in test file but when integrated with Yii application, it starts giving errors. It seems like there is nothing stored in global variables.

Can any one help me out?

Thanks

It’s sort of difficult without any concrete code, but consider using a application component:


class PhpbbBridge extends CApplicationComponent {

...

You just put it in the components array in your main configuration file:


'phpbb_bridge' => array(

	'class' => 'application.components.PhpbbBridge',

),



Then you can refer to it as Yii::app()->phpbb_bridge.

If you could share some of the specific errors that might help solve the problem.

Hi jacmoe

I don`t undertand where put the code

I did the next

In the folder controller i create a file globals.php

tne content is

class globals extends CApplicationComponent {

//put your code here


//public $gblType="Guest";

}

in the file main.php, i write

‘Globals’ => array(

  'gblType' => 'application.components.globals',

),

but I dont understand what is the variable global

and how use it?

Can you help me?

Thank you !

there are params:




Yii::app()->params['xxx'] = 'sss';

...



you can set them also in config file in ‘params’ section. Read here: http://www.yiiframework.com/wiki/126/setting-and-getting-systemwide-static-parameters/

You can work with a simple class and constants/static methods.

Create the class ‘Globals.php’ in the protected/components folder.





class Globals

{

   const defaultUserType='Guest';

   ...


   public static function doSomething($param) 

   {

      if($param == self::defaultUserType)

      ... 

   }


}




Usage:




   

   $user->type = Globals::defaultUserType;


   Globals::doSomething($param);




If you want to work with an application component and config/override the values in config/main.php you can implement this like below.

Create the class ‘Globals.php’ in the protected/components folder





class Globals extends CApplicationComponent 

{

   public $defaultUserType='Guest';

   ...




   public function doSomething($param) 

   {

      

      ... 

   }


}






In config/main.php





  'components'=>array(


        'global'=>array( 

            'class' => 'Globals', //or 'application.components.Globals'

            'defaultUserType' => 'Editor',

        ),

    ...




Usage:




   

  $user->type = Yii::app()->global->defaultUserType;


   Yii::app()->global->doSomething($param);