I am working on a Yii application. I am trying to set some paths in my main config params like this:
// application-level parameters that can be accessed
// using Yii::app()->params['paramName']
'params'=>array(
'paths' => array(
'imageTemp'=> Yii::getPathOfAlias('webroot').'/files/temp-',
'image'=> Yii::getPathOfAlias('webroot').'/files/',
...
),
'urls' => array(
'imageTemp'=> Yii::app()->getBaseUrl().'/files/temp-',
'image'=> Yii::app()->getBaseUrl().'/files/',
...
),
But I am getting this error:
Fatal error: Call to a member function getBaseUrl() on a non-object in ..blahblah../base/CApplication.php on line 553
I think I cannot use Yii::app() in config file since the app is not initialized yet here, or something like this. So, how can I replace Yii::app()->getBaseUrl()
in the config file and get the same results?
To get the base URL of the current request use the following: $relativeBaseUrl = Url::base(); $absoluteBaseUrl = Url::base(true); $httpsAbsoluteBaseUrl = Url::base('https'); The only parameter of the method works exactly the same as for Url::home() .
Yii::app()->request->getUrl(); Yii::app()->request->getParams('id'); $_GET['id'];
Applications are objects that govern the overall structure and lifecycle of Yii application systems. Each Yii application system contains a single application object which is created in the entry script and is globally accessible through the expression \Yii::$app .
You're right, you can't use the Yii::app() methods inside the config's return array, but you can use Yii::getPathOfAlias()
outside. Something like this might work:
$webroot = Yii::getPathOfAlias('webroot');
return array(
...
'params'=>array(
'paths' => array(
'imageTemp'=> $webroot.'/files/temp-',
'image'=> $webroot.'/files/',
...
),
),
);
Assuming webroot
is defined beforehand.
As for baseUrl... I'll come back to you on that one!
[Back...]
If you need a url, it all depends where your images files are being kept, relative to the yii path, or relative to the base of the web root?
If the base of the web root, you could just use:
return array(
...
'urls'=>array(
'paths' => array(
'imageTemp'=> '/files/temp-',
'image'=> '/files/',
...
),
),
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With