Getting base URL in Yii 2 Getting base URL in Yii 2 php php

Getting base URL in Yii 2


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

use yii\helpers\Url;Url::base();         // /myappUrl::base(true);     // http(s)://example.com/myapp - depending on current schemaUrl::base('https');  // https://example.com/myappUrl::base('http');   // http://example.com/myappUrl::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.


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();


To get base URL Yii2 using:

Url::home(true)