In many, many places throughout Magento you will come across uses of getUrl() that generates a web address. It takes just two parameters but is immensely versatile.
Parameter 1 – $routePath
The route path follows the Zend behaviour of “module/controller/action”.
The module part is self-explanatory. For example “cms” resolves to Mage_Cms
.
The controller is the class that will handle the address. “cms/page” refers to the class Mage_Cms_PageController
.
Lastly, the action is the method within that class. “cms/page/view” will callMage_Cms_PageController::viewAction()
.
If any of these parts is given as “*” it will use the current module, controller or action in use. If omitted it defaults to “index”. For example; If viewing a CMS page then the following is equivalent to “cms/page/index”. Note there is no “view”.
-
Mage::getUrl(‘*/*’);
Parts that aren’t recognised are passed verbatim which makes for an easy shortcut. You can pass the path to a static file and it will be appended to the domain.
-
Mage::getUrl(‘index.html’);
Parameter 2 – $routeParams
This is an array that converts key/values into pairs of path directories.
-
Mage::getUrl(‘cms/page/view’, array(‘id’ => 1));
There are several special values that effect the outcome. They all begin with an underscore and are reserved.
_absolute | n/a | No effect. URLs are always generated as absolute. |
_current | bool | Uses the current module, controller, action and parameters |
_direct | string | Simply append to the base URL, same effect as passing to $routePath. See _store |
_escape | bool | Uses & instead of & |
_forced_secure | bool | Uses the secure domain given in configuration |
_fragment | string | The last part of the URL after a # |
_ignore_category | bool | Only applies to Mage_Catalog_Model_Product_Url::getUrl() . Prevents category rewrite from being used. |
_nosid | bool | Prevents a SID query parameter being used when referencing another store |
_query | string or array | If an array it is converted into a string like ?key=value&key=value which will become the$_GET variable. |
_secure | bool | Uses the secure domain if allowed in configuration |
_store | int or string | Either the numeric store ID or textual store code. It will use the correct domain as the base URL. |
_store_to_url | bool | Adds ___store to the query parameters. Useful for targetting a store that doesn’t have an unique domain. |
_type | string | link is the default. direct_link is useful for bypassing the “store code in URLs” feature. js ,media and skin append the domain (and possibly store code) with the relevant directory. |
_use_rewrite | bool | Looks up the module/controller/action/parameters in the database for a search engine friendly equivalent. |
Usage Examples
Current Page
-
Mage::getUrl(”, array(
-
‘_current’ => true,
-
‘_use_rewrite’ => true
-
));
Full path is preserved. Fragment and queries are stripped.
Current Page, Secured, For Another Store
-
Mage::getUrl(”, array(
-
‘_current’ => true,
-
‘_use_rewrite’ => true,
-
‘_secure’ => true,
-
‘_store’ => 2,
-
‘_store_to_url’ => true
-
));
Session ID may well be added automatically to solve the cookie problem.
Static Page On Main Store
-
Mage::getUrl(‘example.html’, array(
-
‘_nosid’ => true,
-
‘_store’ => ‘default’,
-
‘_type’ => ‘direct_link’
-
));
Session ID is stripped, the static will not be using it. A type of direct_link means a store code is not inserted