zu3i4ot
(Zu3i4ot)
April 6, 2011, 8:13am
1
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
mdomba
(Maurizio Domba Cerin)
April 6, 2011, 8:28am
2
You can use ‘params’ in config/main.php
zu3i4ot
(Zu3i4ot)
April 7, 2011, 5:19am
3
params are working well
one problem:
when i use this inside the params
‘imagesURL’=>Yii::app()->request->baseUrl.’/images/’,
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?
zu3i4ot
(Zu3i4ot)
April 7, 2011, 6:34am
4
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
mdomba
(Maurizio Domba Cerin)
April 7, 2011, 7:00am
5
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
mikl
(Mike)
April 7, 2011, 7:56am
6
You could also use path aliases to define filesystem locations related to your application:
'imagesDirectory'=>'webroot.media',
Then when you later want to use that path:
$imageDir=Yii::getPathOfAlias(Yii::app()->params['imagesDirectory']);
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 main.ph is the same
The variable lost the changes.
Example.
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.
Greetings
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\components\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]"$this"[/b].
$this->chkFileExist('./'.$this->productImageFolder.'/');
Hope this will be help.