diegovarussa
4/24/2015 - 12:47 AM

Generate URLs in Magento in .phtml file

Generate URLs in Magento in .phtml file

<?php

Mage::getUrl('*/*');
// http://www.example.com/cms/page/
#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');
// http://www.example.com/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));
// http://www.example.com/cms/page/view/id/1
#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 &amp; 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.