How is the Mage::getUrl() method useful? Why can't I just use a normal relative link?


Mage::getUrl provides one single point for generating urls.
Magento uses everywhere absolute urls.
This way you avoid issues when you have your app installed in a subfolder (and not only).
Using relative paths can lead to errors.
For example <a href="something">Click</a> can lead to one page when on homepage and to an other when on a product page.
There is also an other reason.
each module has an url key declared in the config.xml.
In the example I linked it means that all urls that start with checkout will be mapped to the checkout module.
But you can easily change this key.
Having a single point for generating urls means that Magento can easily handle your change and you don't have to go through all the files and change your urls.
Also, magento allows store codes in urls.
This means that the urls will be properly generated when you have this setting on and off without you doing anything.

That's all I have for now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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