Useful Function To Get The Full URL Path In Magento

back to tech articles
Magento CE 1.8.0.0

I recently needed to build the full URL for Magento from a relative path. I wrote this little function for my helper and I thought someone else might want to use it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public function buildFullPathUrl($path,$type)
{
    $relative = ltrim($path, '/');
    switch ($type) {
        case 'web':
            $url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
            break;
        case 'js':
            $url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS);
            break;
        case 'media':
            $url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
            break;
        case 'skin':
            $url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN);
            break;
        case 'link':
            $url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
            break;
        default:
            $url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
            break;
    }
    $fullUrl = $url . $relative;
    return $fullUrl;
}

// usage
Mage::helper('my_module/data')->buildFullPathUrl('/products.html');

This could be easily expanded to accommodate store views.

That’s it, enjoy.