getBaseUrl() returns empty

Hi there,

I’m using Yii v1.1.4 and when I call it from a view returns an empty string:


Yii::app()->getBaseUrl()

Yii::app()->baseUrl

Is this right?

I think this must return the base domain, right?

Like if my project is under a local vhost called "http://local.myproject.com/index.php", it must return "http://local.myproject.com/".

Thanks in advance,

whether it has been tried in this way:




echo Yii::app()->getBasePath();



Thanks Hermans,

That works and returns the folder path like "…/www/protected…"

What I was trying to get is the url not the path.

I don’t know if I messed up the main.php or something…

Thanks again,

I have upgraded to v1.8:




 <?php echo Yii::app()->getBaseUrl(); ?>

<?php echo Yii::app()->baseUrl; ?>



Returns empty string too.

If I want to set it into /config/main.php it says that is readonly attribute.

Try getBaseUrl() with parameter true

/Tommy

I don’t know if it makes a difference but I have the webapp divided in frontend and backend like so:

http://www.yiiframework.com/wiki/33/

Hi cactork,

      You can get base url as follows

But it will return a relative url (exclusive ending slash), meaning empty string again.

Like tri said

If url is


http://example.com/index.php

then


echo Yii::app()->getBaseUrl(true) . '/';

should print


http://example.com/

Hi there, thanks for the answers!

That’s right Y!!:


<?php echo Yii::app()->getBaseUrl(true) . '/'; ?>

Returns me the "base domain url" like so:


http://www.example.com/

I’ll create a shortcut function for this.

Thanks again for help me with this to each one of you…

good solution for me too

Thanks. this helped me alot!

you can use


Yii::app()->getBaseUrl(true)

That’s the good solution.

echo Yii::app()->getBaseUrl(true) . ‘/’;

is very good solution … thanks Yii

Look into


 Yii::app()->createAbsoluteUrl($route)

p.s. Why do you need it. I’m currently host three vhost sites in house. Two of the vhost sites use the same code base, the index.php changes the theme depending on the which site is being requested.

Links within the site all use Yii::app()->request->baseUrl.’/folder/filename.ext’ and the browser knows which vhost to go to. You are getting an empty string is because the bootstrap file is in the root of the vhost site.

The only reson to use createAbsoluteUrl() is if you are sending an email with a link or generating an RSS feed with links.

Thanks a lot!

This solution works for me.

PS. But I don’t understand, why Yii::app()->baseUrl returns empty string or ‘/’ in my case.

As I know, it should get needed URL automatically unlike basePath.

basePath value is set in config.php.

Maybe that’s because of param $absolute=true: in this case we’ve got absolute URL of our Yii application.