38

I am trying to get the base URL for the project in Yii 2 but it doesn't seem to work. According to this page you used to be able to do:

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

In Yii 1, but it seems that that method in Yii 2 no longer accepts a parameter?

I've tried doing it without true, such as:

Yii::$app->getBaseUrl();

But it just returns empty.

How can you do this in Yii 2?

rob006
  • 18,710
  • 5
  • 41
  • 58
Brett
  • 16,869
  • 50
  • 138
  • 258

10 Answers10

45

To get base URL of application you should use yii\helpers\Url::base() method:

use yii\helpers\Url;

Url::base();         // /myapp
Url::base(true);     // http(s)://example.com/myapp - depending on current schema
Url::base('https');  // https://example.com/myapp
Url::base('http');   // http://example.com/myapp
Url::base('');       // //example.com/myapp

Url::home() should NOT be used in this case. Application::$homeUrl uses base URL by default, but it could be easily changed (for example to https://example.com/myapp/home) so you should not rely on assumption that it will always return base URL. If there is a special Url::base() method to get base URL, then use it.

rob006
  • 18,710
  • 5
  • 41
  • 58
31

My guess is that you need to look at aliases.

Using aliases would be like:

Yii::getAlias('@web');

You can also always rely on one of these two:

Yii::$app->homeUrl;

Url::base();
glennsl
  • 23,127
  • 11
  • 49
  • 65
DiegoCoderPlus
  • 720
  • 5
  • 14
  • 7
    `Url` btw is from `yii\helpers\Url` – u_mulder Jan 09 '15 at 08:43
  • 4
    I tried `Url::home(true)` and `Url::base(true)` and they both pretty much return the same thing, except `Url::home(true)` includes an ending `/`. Is there any difference other than that? – Brett Jan 09 '15 at 11:28
  • Anyone know the difference? Can provide an example? – Brett Jan 10 '15 at 06:50
  • @Brett Yii Framework is totally open and docummented on github, you should check https://github.com/yiisoft/yii2/blob/master/framework/helpers/BaseUrl.php#L293 and compare functions, Url:home relies on URI:scheme to return URL if defined otherwise it will return Yii::$app->getHomeUrl(); in the otherway URL:base is the same when scheme = true but, when scheme is false the url is given by $url = Yii::$app->getUrlManager()->getBaseUrl(); I strongly suggest not to be afraid to check out yii code by yourself, it is amazingly well docummented – DiegoCoderPlus Jan 10 '15 at 11:53
  • Perhaps you could edit your answer to include `Url::base(true)` instead as that's probably the one I will use. – Brett Jan 10 '15 at 15:49
10

To get base URL Yii2 using:

Url::home(true)
Đọc truyện hay
  • 1,611
  • 17
  • 14
  • This is incorrect, `Url::home()` does not always return base URL. See [my answer](https://stackoverflow.com/a/50546628/5812455). – rob006 May 26 '18 at 20:16
7

Use it like this:

Yii::$app->getUrlManager()->getBaseUrl()

More information on base, canonical, home URLs: http://www.yiiframework.com/doc-2.0/yii-helpers-url.html

NovaLogic
  • 630
  • 13
  • 20
  • 3
    Anytime you see `get` as the function base name, it is a `getter` function. Meaning you can just do: `Yii::$app->urlManager->baseUrl`. The functions are ugly and looks like you don't understand the point of getter functions. – Wade Jul 29 '16 at 18:40
  • 4
    @WadeShuler Getters are only syntax sugar with some performance implications - using methods is actually preferred. In framework core they're used almost every time. – rob006 May 26 '18 at 20:20
3

may be you are looking for this

Yii::$app->homeUrl

you can also use this

Url::base().

or this

Url::home();

rahul s negi
  • 129
  • 13
3

Try this:

$baseUrl = Yii::$app->urlManager->createAbsoluteUrl(['/']);
rob006
  • 18,710
  • 5
  • 41
  • 58
Radhe9254
  • 163
  • 1
  • 10
1

You can reach your base URL by this:

Yii::$app->request->baseUrl
rob006
  • 18,710
  • 5
  • 41
  • 58
Thaier Alkhateeb
  • 555
  • 4
  • 8
  • 17
1

I searched for a solution how we can do like in codeigniter, routing like e.g.

base_url()
base_url('profile')
base_url('view/12')

Only way we can do that in Yii2

<?=Url::toRoute('/profile') ?>
Bira
  • 2,485
  • 1
  • 20
  • 35
-1

Try below code. It should work. It will return base URL name

use yii\helpers\Url;

Url::home('http') // http://HostName/ OR Url::home('https') // https://HostName/

-1

In yii 1 this code return the hostname

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

In yii2 the following

Yii::$app->getBaseUrl();

not exists as method of Yii::$app and it triggers an error with message

Calling unknown method: yii\web\Application::getBaseUrl() 

You could use Request class that encapsulates the $_SERVER

Yii::$app->request->hostInfo
vasillis
  • 332
  • 4
  • 9