raghul
raghul

Reputation: 1510

What is the difference between homeUrl and baseUrl in Yii framework?

What is the difference between homeUrl and baseUrl in Yii framework?

Upvotes: 22

Views: 26953

Answers (3)

Einlanzer
Einlanzer

Reputation: 534

Yii::app()->getBaseUrl(true);   // => http://localhost/yii_projects
Yii::app()->getHomeUrl();       // => /yii_projects/index.php
Yii::app()->getBaseUrl(false);  // => /yii_projects

This is the best practice in pointing directory of images etc. or the file itself rather than hardcoding the path which can change name"

Upvotes: 28

Nimir
Nimir

Reputation: 5839

From the docs:

baseUrl:

Returns the relative URL for the application. This is similar to scriptUrl except that it does not have the script file name, and the ending slashes are stripped off.

While, homeUrl is

the homepage URL

Try echoing somewhere at your application to examine each by yourself:

echo Yii::app()->getBaseUrl(true);// true tells to get a relative url, false the other way
echo Yii::app()->getHomeUrl();

How to use each?

baseUrl is as @bcmcfc said can be useful as a base for all links in your applications.

Now imagine you wanted to link to an image in web_root/myapp/img/ if for example you did that using absolute path e.g <img src="C:/wwww/myapp/img/somepic.jpg> Let's say you finished all your development and now you want to deploy to some linux server!!

You can see that all your links will be broken :( but if instead you did: <img src= <?php Yii::app()->baseUrl() ?> . "/img/somepic.jpg" Everything should work fine :)

homeUrl is simply the landing page for your app. Well i didn't use this before but i guess you can set diffirent homeurls according to User role after login for example!!

Upvotes: 18

bcmcfc
bcmcfc

Reputation: 26795

One is the base URL for the links in your app. The other is the home URL. The home URL would be based on the base URL and would be a particular route or page, for example, that is redirected to after logging in.

Upvotes: 1

Related Questions