cactork
(Martin Hansen)
July 31, 2011, 11:31pm
1
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,
h3rm
August 1, 2011, 1:51am
2
whether it has been tried in this way:
echo Yii::app()->getBasePath();
cactork
(Martin Hansen)
August 1, 2011, 3:59am
3
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,
cactork
(Martin Hansen)
August 6, 2011, 7:05pm
4
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.
tri
(tri - Tommy Riboe)
August 6, 2011, 7:31pm
5
cactork:
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
cactork
(Martin Hansen)
August 6, 2011, 7:32pm
6
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
Y11
(Y!!)
August 7, 2011, 9:55am
8
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/
cactork
(Martin Hansen)
August 7, 2011, 4:44pm
9
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…
markyii
(Mark Roy Obre)
April 26, 2013, 7:39am
11
Thanks. this helped me alot!
you can use
Yii::app()->getBaseUrl(true)
ganeshbora
(Boraganesh)
April 26, 2013, 10:00am
13
cactork:
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…
That’s the good solution.
kapous
(Hatemn2001)
April 23, 2014, 6:29am
14
echo Yii::app()->getBaseUrl(true) . ‘/’;
is very good solution … thanks Yii
jkofsky
(Jkofsky)
April 24, 2014, 1:46am
15
Look into
Yii::app()->createAbsoluteUrl($route)
jkofsky
(Jkofsky)
April 24, 2014, 2:04am
16
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.