27

Is there a reason to use Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB) rather than Mage::getBaseUrl()? I've seen both in use, and they both return the same thing. Is the longer one redundant? It uses 2 function calls, as well.

Moreover, I see that Mage_Core_Model_Store::URL_TYPE_WEB simply returns the string 'web'. What's the point of it?

1 Answer 1

42

First look at getBaseUrl() at Mage.php

 public static function getBaseUrl($type = Mage_Core_Model_Store::URL_TYPE_LINK, $secure = null)
    {
        return self::app()->getStore()->getBaseUrl($type, $secure);
    }

In a simple word we can say:

Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB) indicated Your Magento Site Url from this url Magento is running it System.

And other word ,we can say that Mage::getBaseUrl() give the url which is linking your magento internal page urls That MVC page

Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB):

Value Set at : From Admin>System>Configuration>WebSite>Secure/Unsecure -> Base url

Mage::getBaseUrl():

Value Set at : From Admin>System>Configuration>WebSite>Secure/Unsecure -> Base Link url

Main Difference Showing:

Whenever we are enable Use Web Server Rewrites = no from admin>system>Web>Search Engines Optimization Rewrites

Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB) give the value of http://youdomain/

but Mage::getBaseUrl() give value http://youdomain/index.php/ that actually give the value of Mage_Core_Model_Store::URL_TYPE_LINK

when you hit http://youdomain/ that is call Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB) url But that times you can see all pagelink of your site is starting with http://youdomain/index.php/ that means it taken Mage::getBaseUrl()/ Mage::getBaseUrl($type = Mage_Core_Model_Store::URL_TYPE_LINK, $secure = null)

Example:

If you have create a folder amit at Magento root dir and there you have put a file test.js then you want to call this file at magento home then you can use this code

<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB).'amit/test.js'; ?>

and it give then url

www.yourdomain/amit/test.js

If You use only Mage::getBaseUrl() which give u www.yourdomain/index.php/amit/test.js that mean test.js cannot accessible (when rewite disable)

But you have create new controller and new action then you can use

Mage::getBaseUrl().'yourmodulerouter/yourcontroller/youraction'

it show  url like `http://yourdomain/index.php/yourmodulerouter/yourcontroller/youraction`

or give http://yourdomain/yourmodulerouter/yourcontroller/youraction(when rewrite enable)

6
  • So Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB) will work better when we have rewrites turned on? Nov 6, 2014 at 21:52
  • no.Mage::getBaseUrl():is always use for magento pageliks
    – Amit Bera
    Nov 7, 2014 at 4:41
  • Thanks for helping. But you said that Mage::getBaseUrl() gives you a link that has index.php in it. Why would I want that extra index.php if I don't need it? Nov 7, 2014 at 6:21
  • If i send do not send any parameter on function getBaseUrl() then it send if params as $type = Mage_Core_Model_Store::URL_TYPE_LINK, $secure = null)
    – Amit Bera
    Nov 7, 2014 at 6:32
  • pleas check this magento Mage.php
    – Amit Bera
    Nov 7, 2014 at 6:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.