Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 470
raghul Avatar asked Sep 19 '12 12:09

raghul


2 Answers

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"

like image 111
Einlanzer Avatar answered Oct 19 '22 20:10

Einlanzer


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!!

like image 23
Nimir Avatar answered Oct 19 '22 19:10

Nimir