문제

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');
도움이 되었습니까?

해결책

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

다른 팁

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;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top