0

What I need is pass some json data to grid block and display it in a grid. I have created admin module and successfully recieve json data. But I dont know how to pass json to block.

class Somecompany_Modulename_Adminhtml_TestController extends Mage_Adminhtml_Controller_Action
{
    public function pollAction() {  
        $json = $this->_getCurlData();
        Mage::getSingleton("adminhtml/session")->addSuccess($json);         
        $this->loadLayout();    
        return $this->renderLayout();
    }
    private function _getCurlData() {
        $id = $this->getRequest()->getParam('print_id');
        $url = "https://my.sample.url";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);                               
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $json = curl_exec($ch);
        curl_close($ch);
        return $json;
    }

}

How to pass this data to block?

Tried this. But I think there should be another way.

Mage::register('name',$var);

2 Answers 2

0

in controller

 Mage::register('json_data', $json);

getting the data in block

if ( Mage::registry('json_data') )
 {

    $data=Mage::registry('json_data');
  }
4
  • I thought there's a better way. Thanks @Qaisar
    – Dushan
    Commented Feb 18, 2016 at 6:04
  • this is the better way magento use this and also i recommended use this. Commented Feb 18, 2016 at 6:22
  • When json is huge does it cause any performance issues?
    – Dushan
    Commented Feb 18, 2016 at 6:50
  • no it is not causing any issue. magento store collection in that. it is destroy after the page load automatically. Commented Feb 18, 2016 at 6:53
0

Try This

class Somecompany_Modulename_Adminhtml_TestController extends Mage_Adminhtml_Controller_Action
{
    public function pollAction() 
    {  
        $json = $this->_getCurlData();

        $this->loadLayout();    
        $this->getLayout()->getBlock('Your_Block')->setJsonData($json);
        return $this->renderLayout();
    }
    private function _getCurlData() 
    {
        $id = $this->getRequest()->getParam('print_id');
        $url = "https://my.sample.url";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);                               
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $json = curl_exec($ch);
        curl_close($ch);
        return $json;
    }

and in your block file you can access like

$this->getJsonData();
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.