Brett
Brett

Reputation: 20049

Getting base URL in Yii 2

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?

Upvotes: 40

Views: 121793

Answers (11)

Prashant Srivastava
Prashant Srivastava

Reputation: 1

Yii::$app->request->baseUrl

Upvotes: 0

Neeraj Maurya
Neeraj Maurya

Reputation: 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/

Upvotes: -1

vasillis
vasillis

Reputation: 342

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

Upvotes: -1

Radhe9254
Radhe9254

Reputation: 198

Try this:

$baseUrl = Yii::$app->urlManager->createAbsoluteUrl(['/']);

Upvotes: 4

Thaier Alkhateeb
Thaier Alkhateeb

Reputation: 585

You can reach your base URL by this:

Yii::$app->request->baseUrl

Upvotes: 2

rob006
rob006

Reputation: 22144

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.

Upvotes: 53

DiegoCoderPlus
DiegoCoderPlus

Reputation: 770

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

Upvotes: 34

Bira
Bira

Reputation: 5506

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') ?>

Upvotes: 2

Đọc truyện hay
Đọc truyện hay

Reputation: 2023

To get base URL Yii2 using:

Url::home(true)

Upvotes: 11

rahul s negi
rahul s negi

Reputation: 139

may be you are looking for this

Yii::$app->homeUrl

you can also use this

Url::base().

or this

Url::home();

Upvotes: 3

NovaLogic
NovaLogic

Reputation: 678

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

Upvotes: 7

Related Questions