Set or Get request variables in Magento
Get Request params like this:
Get every post variable in array like this:
1
2
3
4
5
| $postData = Mage::app()->getRequest()->getPost(); // You can access individual variables like... if (isset( $postData [ 'product_id' ])) { echo $postData [ 'product_id' ]; } |
$this->getRequest()->getParams()
to get all Parameters.$this->getRequest()->getParam(KEY)
to get any specifically.
//
$_POST['deviceid']
$deviceid = $this->getRequest()->getPost('deviceid');
Here, a URL like
http://www.example.com/artists/index/view/id/64
can be used to view the profile for any artist, the artist_id is simply substituted into your query string after the /id/
section. Now is the clever part, to access this value in your controller for viewAction()
, you simply use the following function:$artistid = (int)$this->getRequest()->getParam('id');
$id = (int)$this->getRequest()->getParam('id');
$_GET, $_POST & $_REQUEST Variables in Magento
Accessing Get Variables – $_GET
// This...
$productId = $_GET['product_id'];
$productId = $_GET['product_id'];
// ...is the same as ...
$productId = Mage::app()->getRequest()->getParam(‘product_id’);
$productId = Mage::app()->getRequest()->getParam(‘product_id’);
// The second parameter to getParam allows you to set a default value
// The default value is returned if the GET value you’re looking for isn’t set
$productId = Mage::app()->getRequest()->getParam(‘product_id’, 44);
// The default value is returned if the GET value you’re looking for isn’t set
$productId = Mage::app()->getRequest()->getParam(‘product_id’, 44);
?>
Accessing Post Variables – $_POST
// This...
$postData = $_POST;
$postData = $_POST;
// ...is the same as...
$postData = Mage::app()->getRequest()->getPost();
$postData = Mage::app()->getRequest()->getPost();
// You can access individual variables like…
if (isset($postData['product_id'])) {
echo $postData['product_id'];
}
if (isset($postData['product_id'])) {
echo $postData['product_id'];
}
?>
Get a parameter value:
$paramValue = $this->getRequest()->getParam($paramName);
Get all parameters and values:
$allParams = $this->getRequest()->getParams();
Check if customer is logged in
<?php $logged_in = Mage::getSingleton('customer/session')->isLoggedIn(); // (boolean) ?>
Get the current category/product/cms page
<?php
$currentCategory = Mage::registry('current_category');
$currentProduct = Mage::registry('current_product');
$currentCmsPage = Mage::registry('cms_page');
?>
$_GET, $_POST & $_REQUEST Variables
<?php
// $_GET
$productId = Mage::app()->getRequest()->getParam('product_id');
// The second parameter to getParam allows you to set a default value which is returned if the GET value isn't set
$productId = Mage::app()->getRequest()->getParam('product_id', 44);
$postData = Mage::app()->getRequest()->getPost();
// You can access individual variables like...
$productId = $postData['product_id']);
?>