getUrl function parameters

Last modified by demonkoryu on Tue, January 10, 2012 11:34
Source|Old Revisions  

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 call Mage_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”.

  1. Mage::getUrl('*/*');
  2. //

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.

  1. Mage::getUrl('index.html');
  2. //

Parameter 2 - $routeParams

This is an array that converts key/values into pairs of path directories.

  1. Mage::getUrl('cms/page/view', array('id' => 1));
  2. //

There are several special values that effect the outcome. They all begin with an underscore and are reserved.

Key Type Meaning
_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

  1. Mage::getUrl('', array(
  2.     '_current' => true,
  3.     '_use_rewrite' => true
  4. ));

Full path is preserved. Fragment and queries are stripped.

Current Page, Secured, For Another Store

  1. Mage::getUrl('', array(
  2.     '_current' => true,
  3.     '_use_rewrite' => true,
  4.     '_secure' => true,
  5.     '_store' => 2,
  6.     '_store_to_url' => true
  7. ));

Session ID may well be added automatically to solve the cookie problem.

Static Page On Main Store

  1. Mage::getUrl('example.html', array(
  2.     '_nosid' => true,
  3.     '_store' => 'default',
  4.     '_type' => 'direct_link'
  5. ));

Session ID is stripped, the static will not be using it. A type of direct_link means a store code is not inserted.