php Magento getParam v $_GET
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13533936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Magento getParam v $_GET
提问by Marty Wallace
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
v
$getParam = $_GET['getparam'];
回答by Joseph at SwiftOtter
There is a significant difference between the two. $_GETis 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:
两者之间存在显着差异。$_GET只是一个数组,如$_POST. 但是,调用Mage::app()->getRequest()->getParam('param_name')将使您同时访问 GET 和 POST (此处不包括 DELETE 和 PUT)- 请参阅下面的代码:
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().
此外,如果系统使用 设置其他参数Mage::app()->getRequest()->setParam(),则可以通过该getParam()函数访问它。在 Magento 中,您希望始终使用getParam().
回答by E_p
Mage::app()->getRequest()->getParam('getparam');
Will return you 'getparam' if it is send with GET, POST (not sure about DELETE, PUT ...) request. Did not work with Magento but if there parameters that sent through routing. I would expect them also being accessible through that function.
如果它是通过 GET、POST(不确定 DELETE、PUT ...)请求发送的,将返回您 'getparam'。不适用于 Magento,但如果有通过路由发送的参数。我希望它们也可以通过该功能访问。
$_GETcontains only parameters sent through GET
$_GET只包含通过 GET 发送的参数
$_POSTcontains only parameters sent through POST
$_POST只包含通过 POST 发送的参数

