Magento getParam v $_GET

Marty Wallace picture Marty Wallace · Nov 23, 2012 · Viewed 50.8k times · Source

Can anyone explain the differences both functionally and in terms of good/bad practice whhy one of these should be preferred over the other:

$getParam = Mage::app()->getRequest()->getParam('getparam');

v

$getParam = $_GET['getparam'];

Answer

Joseph at SwiftOtter picture Joseph at SwiftOtter · Nov 23, 2012

There is a significant difference between the two. $_GET is simply an array, like $_POST. However, calling Mage::app()->getRequest()->getParam('param_name') will give you access to both GET and POST (DELETE and PUT are not included here) - see code below:

lib/Zend/Controller/Request/Http.php

public function getParam($key, $default = null)
{
    $keyName = (null !== ($alias = $this->getAlias($key))) ? $alias : $key;

    $paramSources = $this->getParamSources();
    if (isset($this->_params[$keyName])) {
        return $this->_params[$keyName];
    } elseif (in_array('_GET', $paramSources) && (isset($_GET[$keyName]))) {
        return $_GET[$keyName];
    } elseif (in_array('_POST', $paramSources) && (isset($_POST[$keyName]))) {
        return $_POST[$keyName];
    }

    return $default;
}

In addition, if the system sets other params with Mage::app()->getRequest()->setParam(), it becomes accessible via the getParam() function. In Magento you want to always use getParam().