Magento 1.8 – How to Get Data from URL and Pass to Block Method

blockscontrollersmagento-1.8

I need to get data from url and pass it block method for some operation.
Basically i need to list some content on specific id in magento.

my controller function is
----------------------------

        public function showArchiveAction() {

            $id = $this->getRequest()->getParams('o');
            $this->loadLayout();
            $this->renderLayout();
          }

my block method
----------------
public function getMagazine($id)
     {

      $defaultMagazine = Mage::getModel('magazine/display')->getCollection()->load($id);
      $data = $defaultMagazine->getData();
      $defaultMagazineId = $data[0]['magazine_id'];
      $collection = Mage::getModel('magazine/page')->getCollection()->addFieldToFilter('magazine_id',$defaultMagazineId);

      return $collection;
      }

Best Answer

It seams like you are getting the id right.
You have 2 options here to send the data to the block.
Option 1 - Using register.

public function showArchiveAction() {
    $id = $this->getRequest()->getParams('o');
    Mage::register('current_magazine_id', $id);//register the id
    $this->loadLayout();
    $this->renderLayout();
}

Then you can read the id in the block or the template:

$id = Mage::registry('current_magazine_id');
$magazine = $this->getMagazine($id);

Option 2 - assigning the id to the block.
For this approach let's say that you block has the name magazine_details in the layout.

public function showArchiveAction() {
    $id = $this->getRequest()->getParams('o');
    $this->loadLayout();
    $block = $this->getLayout()->getBlock('magazine_details');
    if ($block) {
         $block->setMagazineId($id);
    }
    $this->renderLayout();
}

Then you can read this value in the block or template like this:

$id = $this->getMagazineId();
$magazine = $this->getMagazine($id);
Related Topic