how to create a global variable and where to create it?

i want to create a global variable for the site’s upload directory location how can i create it and where can i create it.

one option in mind is the use of setState()

but can’t figure out where to set it to use it for the whole site with/without login required

You can use ‘params’ in config/main.php

params are working well

one problem:

when i use this inside the params


it says

Notice: Trying to get property of non-object in C:\…\protected\config\main.php

but when i use Yii::app()->request->baseUrl in model it works fine <_<

what is the problem?

i m using

        'imagesDirectory'=>dirname(dirname(__FILE__).DIRECTORY_SEPARATOR.'..'). '/../../media/images/',

in the params array in config main.php

is this good or a bettor way exists to get full path to directory and define it as a global variable ???

The error means that the Yii object is not yet created at that time… so you need to set the folder manually… like you done in the second post

You could also use path aliases to define filesystem locations related to your application:


Then when you later want to use that path:


Hi Friend Maurizio Domba

I have the same trouble.

When I use global to define variables, in some files, the info not is keepit

I want to say that i want change the value of this variable and I can doit.

With "params" in cofig is the same

The variable lost the changes.


In the view "main" of layout i assing a value to variable global o parameter

When i try to use this value, by example in te action "view" the info is lost.

Don’t keep the information.

I think that "param" is an "Constant" but not a variable

Can you Help me?

Thank you in advice.


Hi Friends,

If you want access your define variables in config/main.php than follow the steps.

‘params’=>array(‘productImage’ => ‘/imagefolder/’,),

Step 1

In your protected&#092;components&#092;Controller.php do the following things.


       class Controller extends CController{

            public $productImageFolder = ''; // declare the product image folder variable with value as empty

            public function init(){

         	$this->productImageFolder = YII::app()->params['productImage'];//assign the config variable value.




Step 2

 Through init function in Component controller, we can access that value in any controller.

 In your controller access that variable with [b]&quot;&#036;this&quot;[/b].




Hope this will be help. :rolleyes: