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.