Set or Get request variables in Magento

Posted on September 15, 2010 with 6 Comments

Get Request params like this:

$productId = Mage::app()->getRequest()->getParam('product_id', 44);

Get every post variable in array like this:

$postData = Mage::app()->getRequest()->getPost();
// You can access individual variables like...
if (isset($postData['product_id'])) {
  echo $postData['product_id'];
}

Tags: , ,

Category: Magento



Comments (6)

 

  1. Nicolas says:

    Is it safe to use Mage::app()->getRequest()->getParam(‘product_id’, 44);? Does Magento clean-up the variable (SQL Injection and stuff)?

    Thank

  2. srinivas says:

    If you want set request parameter, you have to use setParam method instead getParam

    Mage::app()->getRequest()->getParam(‘product_id’, 44);

    Replace:

    Mage::app()->getRequest()->setParam(‘product_id’, 44);

  3. Farhan says:

    Hmm,

    The use of $productId = Mage::app()->getRequest()->getParam(‘product_id’, 44); requires a little clarification.

    Can you confirm the second parameter “44″ will be return in case of no parameter or value is set to GET["product_id"] like the way its being managed in CodeIgniter?

    Thanks
    Farhan Islam

  4. nisha says:

    lovely!!

  5. nisha says:

    What is 44 indicating?