Yii - how to access the base URL in the main configuration

I am working on a Yii application. I am trying to set some paths in my basic configuration options as follows:

// 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 get 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 the configuration file, since the application is not yet initialized here, or something like that. So, how can I replace Yii::app()->getBaseUrl() in the configuration file and get the same results?

+6
source share
1 answer

You are right, you cannot use the Yii :: app () methods inside the array of returned configurations, but you can use Yii::getPathOfAlias() outside. Maybe something like this:

 $webroot = Yii::getPathOfAlias('webroot'); return array( ... 'params'=>array( 'paths' => array( 'imageTemp'=> $webroot.'/files/temp-', 'image'=> $webroot.'/files/', ... ), ), ); 

Assuming webroot is predefined.

As for baseUrl ... I will get back to you on this!

[Back ...]

If you need a URL, it all depends on where your image files are stored, relative to the yii path or relative to the base of the web root?

If the base is the root website, you can simply use:

 return array( ... 'urls'=>array( 'paths' => array( 'imageTemp'=> '/files/temp-', 'image'=> '/files/', ... ), ), ); 
+5
source

All Articles