What is second argument in getRequest()->getPost()
-
06-10-2020 - |
Question
What is the use of second argument in getRequest()->getPost
?
$products = $this->getRequest()->getPost('products', -1);
$products = $this->getRequest()->getPost('products', 1);
$products = $this->getRequest()->getPost('products');
La solution
The second argument in getRequest()->getPost
indicates which value to return if there is no variable found with used key value in $_POST
data.
When you use
$products = $this->getRequest()->getPost('products', 1);
It will return you Post data variable with key products
if it exists and if its not available it will return you with the second argument.
If you dont pass any second argument like
$products = $this->getRequest()->getPost('products');
It will return you null
if key value is not set.
Please refer to lib\Zend\Controller\Request\Http.php::getPost()
Autres conseils
It's the default value to use if the key is not found in the $_POST
variable:
/**
* Retrieve a member of the $_POST superglobal
*
* If no $key is passed, returns the entire $_POST array.
*
* @todo How to retrieve from nested arrays
* @param string $key
* @param mixed $default Default value to use if key not found
* @return mixed Returns null if key does not exist
*/
public function getPost($key = null, $default = null)
{
if (null === $key) {
return $_POST;
}
return (isset($_POST[$key])) ? $_POST[$key] : $default;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange