问题描述
任何人都可以从功能上和良好/不良习惯方面解释差异,为什么其中之一应优先于其他:
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'];
推荐答案
两者之间存在显着差异. $_GET
只是一个数组,例如$_POST
.但是,调用Mage::app()->getRequest()->getParam('param_name')
将使您可以同时访问GET和POST (此处不包括DELETE和PUT)-请参见下面的代码:
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;
}
此外,如果系统使用Mage::app()->getRequest()->setParam()
设置其他参数,则可以通过getParam()
功能对其进行访问.在Magento中,您希望始终使用getParam()
.
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()
.
这篇关于Magento getParam v $ _GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!