9

When I do

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

I get an empty string. A post on the Yii forum says this is blank by default. How can I change its default value so that I can use absolute URLs?

tereško
  • 58,060
  • 25
  • 98
  • 150
David Xia
  • 5,075
  • 7
  • 35
  • 52
  • which yii version? and directory where the project is? is it a webapp? have u used yiic ? – bool.dev Sep 30 '11 at 17:28
  • @DroidUser Yii Version 1.1.7. My directory tree is this: `/htdocs/protected/(web application files)` `/lib/yii/(yii core files)` It is a webapp. I haven't used yiic. Is there a way to set baseUrl by running that? I didn't do the initial install of the Yii framework. – David Xia Sep 30 '11 at 19:30
  • in my answer i have mentioned how to run yiic, you do not have to install yii as such, just put it in a web accessible folder, as mentioned in the readme that comes. – bool.dev Oct 01 '11 at 06:54
  • here is my collection http://suriyanphp.blogspot.in/2012/06/yii-resources.html – Suriyan Suresh Jun 25 '12 at 08:38
  • For anyone searching how to *get* the base URL, the code from [here](http://stackoverflow.com/a/14000602/247893) works wonders: `Yii::app()->createAbsoluteUrl(Yii::app()->request->url)`. – h2ooooooo Mar 24 '14 at 12:29

7 Answers7

33

You can edit /path/to/application/protected/config/main.php to change the default value. Add a request component, and configure baseUrl porperty.

return array(
    ...
    'components' => array(
        ...
        'request' => array(
            'baseUrl' => 'http://www.example.com',
        ),
    ),
);
ecco
  • 506
  • 1
  • 4
  • 6
12

As the post in that forum says, it might be different for different platforms, or if the web app isnt located in the default folder.
All these things work 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 the web app, with default settings using the following command in a terminal, yiic webapp /path/to/webapp
So that generates the necessary directory structure for the web app, and also the default skeleton files. Try it and then see how it works. I'm new to yii myself.

Edit:

This solution might have worked for the op, but the correct way baseUrl can be set is shown by ecco's answer to this question.

Community
  • 1
  • 1
bool.dev
  • 17,508
  • 5
  • 69
  • 93
  • `var_dump(Yii::app()->request->baseUrl); var_dump(Yii::app()->getBaseUrl(true)); var_dump(Yii::app()->request->getBaseUrl(true));` Gives me string(0) "", string(25) "http://www.MYURL.com", string(25) "http://www.MYURL.com" Interesting – David Xia Oct 01 '11 at 18:05
  • so did u use yiic to recreate your project? or u got these with your original config?, plus `Yii::app()->baseUrl` also works. what u are getting IS interesting – bool.dev Oct 01 '11 at 19:21
  • I didn't start our Yii project so I don't know. But I'll ask my team. Why do you suppose I got the above output? – David Xia Oct 01 '11 at 19:27
  • sorry, i dont know about the output, but one thing im pretty sure is that if you use yiic, it should be fine. – bool.dev Oct 01 '11 at 21:19
  • It's based on the location of your bootstrap file. – ldg Oct 02 '11 at 01:43
3

It's most likely blank because your bootstrap file (index.php) is at your web root. If it wasn't, you should see some value. Changing it would defeat it's purpose.

You would use like:

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

which for most cases wouldn't change the path, but if you did, say, decide to put your Yii app inside a subdirectory, then it would be portable. (Simply remove the http method and hostname above to make it work on the same host the user is on.)

ldg
  • 9,112
  • 2
  • 29
  • 44
1

Yii::app()->baseUrl returns just a relative Path from the url to index.php for example: 127.0.0.1/index.php returns '' 127.0.0.1/yii/index.php returns '/yii'

balrok
  • 444
  • 3
  • 8
0

If you are using Url helper to generate urls, you will need to set baseUrl key to UrlManager component

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

Then you can create absolute url by using Url helper as,

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

Sachin G.
  • 1,870
  • 19
  • 24
0

try echo Yii::$app->baseUrl(true)
Note that the true parameter is what does the needful.

Kushagra
  • 626
  • 6
  • 20
0

Using Yii 2.0.13.1 the below code is working fine

<?php echo Yii::$app->request->baseUrl; ?>
Anoop Saini
  • 307
  • 3
  • 5