Magento get url with or without parameters

Posted by Damodar Bashyal on May 13, 2013

 

Magento lets you get url with or without parameters as below:

// get base url
Mage::getUrl()
// http://dltr.org/

// get base url and index.html page
Mage::getUrl('index.html')
// http://dltr.org/index.html

// get brands page with id parameter
Mage::getUrl('brands/brand', array('id' => 1));
// http://dltr.org/brands/brand/id/1

// get secure url
Mage::getUrl('brands/brand', array('id' => 1,'_secure' => true));
// https://dltr.org/brands/brand/id/1

// or get secure url based on current page. good for ajax urls.
Mage::getUrl('brands/brand', array('id' => 1,'_secure' => Mage::app()->getStore()->isCurrentlySecure()));
// http(s)?://dltr.org/brands/brand/id/1

...

You can pass a lot more params:

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.

 

Sources:

app\Mage.php:getUrl()

    /**
     * Generate url by route and parameters
     *
     * @param   string $route
     * @param   array $params
     * @return  string
     */
    public static function getUrl($route = '', $params = array())
    {
        return self::getModel('core/url')->getUrl($route, $params);
    }

app\code\core\Mage\Core\Model\Url.php:getUrl()

    /**
     * Build url by requested path and parameters
     *
     * @param   string|null $routePath
     * @param   array|null $routeParams
     * @return  string
     */
    public function getUrl($routePath = null, $routeParams = null)
    {
        $escapeQuery = false;

        /**
         * All system params should be unset before we call getRouteUrl
         * this method has condition for adding default controller and action names
         * in case when we have params
         */
        if (isset($routeParams['_fragment'])) {
            $this->setFragment($routeParams['_fragment']);
            unset($routeParams['_fragment']);
        }

        if (isset($routeParams['_escape'])) {
            $escapeQuery = $routeParams['_escape'];
            unset($routeParams['_escape']);
        }

        $query = null;
        if (isset($routeParams['_query'])) {
            $this->purgeQueryParams();
            $query = $routeParams['_query'];
            unset($routeParams['_query']);
        }

        $noSid = null;
        if (isset($routeParams['_nosid'])) {
            $noSid = (bool)$routeParams['_nosid'];
            unset($routeParams['_nosid']);
        }
        $url = $this->getRouteUrl($routePath, $routeParams);
        /**
         * Apply query params, need call after getRouteUrl for rewrite _current values
         */
        if ($query !== null) {
            if (is_string($query)) {
                $this->setQuery($query);
            } elseif (is_array($query)) {
                $this->setQueryParams($query, !empty($routeParams['_current']));
            }
            if ($query === false) {
                $this->setQueryParams(array());
            }
        }

        if ($noSid !== true) {
            $this->_prepareSessionUrl($url);
        }

        $query = $this->getQuery($escapeQuery);
        if ($query) {
            $mark = (strpos($url, '?') === false) ? '?' : ($escapeQuery ? '&' : '&');
            $url .= $mark . $query;
        }

        if ($this->getFragment()) {
            $url .= '#' . $this->getFragment();
        }

        return $this->escape($url);
    }

...

Farhan Islam posted on - Thursday 5th of February 2015 06:18:02 AM

Can you give me any hint what does core/store model does with parameter true/false for getCurrentUrl ?
 
not published on website


QR Code: Magento get url with or without parameters