Mage::getBaseUrl() vs Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB)

  • 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?

  • Amit Bera

    Amit Bera Correct answer

    7 years ago

    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)

    So `Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB)` will work better when we have rewrites turned on?

    no.Mage::getBaseUrl():is always use for magento pageliks

    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?

    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)

    pleas check this magento Mage.php

    @AmitBera I am not sure if this is correct. getBaseUrl() return in my case http://dev.magento18.com/default/ while Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_DIRECT_LINK) returns http://dev.magento18.com/

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM