What is the difference between homeUrl and baseUrl in Yii framework?
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"
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!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With