Pergunta

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');
Foi útil?

Solução

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()

Outras dicas

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;
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange
scroll top