How to set base url in Yii Framework

When i do

print_r(Yii::app()->request->baseUrl) 

I get an empty string. The Yii forum post says it is empty by default. How to change the default value to use absolute urls?

+7
source share
6 answers

As stated in a message on this forum, it may be different for different platforms or if the web application is not in the default folder.
All this works for me:

 echo Yii::app()->request->baseUrl."<br/>" ; print_r(Yii::app()->request->baseUrl); echo "<br/>"; var_dump(Yii::app()->getBaseUrl(true)); echo "<br/>"; echo Yii::app()->request->getBaseUrl(true); 

I used yiic to create a web application with default settings using the following command in the terminal, yiic webapp /path/to/webapp
This creates the necessary directory structure for the web application, as well as the default skeleton files. Try it and then see how it works. I am new to yii.

Edit:

This solution might work for op, but the correct way baseUrl could be set ecco answer to this question .

+12
source

You can modify /path/to/application/protected/config/main.php to change the default value. Add a query component and configure basicUrl porperty.

 return array( ... 'components' => array( ... 'request' => array( 'baseUrl' => 'http://www.example.com', ), ), ); 
+31
source

Most likely empty because your bootstrap file (index.php) is in your web root. If this is not the case, you should see some value. His change will surpass his goal.

You would use like:

 href="<?php echo 'http://www.myhost.com' . Yii::app()->request->baseUrl; ?>/css/screen.css" 

which in most cases will not change the path, but if, say, you decide to place your Yii application in a subdirectory, then it will be portable. (Just remove the http method and the hostname above so that it works on the same host the user is on.)

+2
source

Yii :: app () โ†’ baseUrl returns only the relative path from url to index.php for example: 127.0.0.1/index.php returns '' 127.0.0.1/yii/index.php returns '/ yii'

+1
source

If you use the Url helper to generate URLs, you will need to set the baseUrl key for the UrlManager component

  'urlManager' => [ 'baseUrl' => 'http://example.com', 

Then you can create an absolute URL using the Url as helper,

echo Url::to(['site/index', 'src' => 'ref1', '#' => 'name'], true); // output ===> http://example.com/index.php?r=site%2Findex&src=ref1#name

0
source

try echo Yii::$app->baseUrl(true)
Note that a true parameter is what you need.

0
source

All Articles