1

I had written a observer to trap product view page event and it is working fine.Now in that code i want to call a controller and pass the product id in it and render the custom view.phtml file, so how can i call that controller without changing my current browser url and render the view.phtml file on same url.

My Observer Code is Given Below

public function getProductView(Varien_Event_Observer $observer)
{         
      $resp = $observer->getEvent()->getFront()->getAction()->getRequest();
      $requestUri = $resp->getRequestString();            
       if ($resp->getActionName() == 'noRoute'){
           $requestUri = $resp->getRequestString();
           $requestUri = ltrim($requestUri, '/');
           $this->db = Mage::getSingleton('core/resource')->getConnection('core_write');
           $sql = "SELECT * FROM core_url_rewrite WHERE request_path like '%".$requestUri."%'";
           $data = $this->db->fetchAll($sql);
           // header("Location: " . Mage::getBaseUrl() . "customproduct/prodId=".$data[0]['product_id'], 301);
           // exit;

           //header('location:'.MAGE::getBaseurl()."customproduct",301);

           //Edit: action not passed in to this event; passed in generic generate_blocks event
             //Mage::getUrl('customproduct');
           exit;
       }
    }
1

You can do this by below code:

$identifier=trim($resp->getPathInfo(),'/');
    $resp
    ->setModuleName('ModuleRouterName')  
                ->setControllerName('controllerName')
                ->setActionName('YOurActioname');

//$resp->setDispatched(true);  

$resp->setAlias(
                Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
                $identifier
        );
return true;

setDispatched(true/false) not sure..

  • Hi @Amit Bera thanks for your reply i have tried your solution $resp->setModuleName('customproduct') ->setControllerName('Index') ->setActionName('index')->setDispatched(true); but it is giving me white screen – RickyN73 Jun 26 '15 at 6:49
  • update ..check now – Amit Bera Jun 26 '15 at 6:56
  • Hi @ Amit Bera, I tried this suggestion however Index controller is not called. When I go to browser and type {domain}/customproduct/index then index controller is getting called and I can see messge that has been echo'ed. However When we trap the event in observer, then from there index controller is not being called. – RickyN73 Jun 26 '15 at 7:07
  • Part of the var_dump of $resp is protected '_dispatched' => boolean true protected '_module' => string 'customproduct' (length=13) protected '_moduleKey' => string 'module' (length=6) protected '_controller' => string 'index' (length=5) protected '_controllerKey' => string 'controller' (length=10) protected '_action' => string 'index' (length=5) protected '_actionKey' => string 'action' (length=6) – RickyN73 Jun 26 '15 at 7:09
  • Hi @ Amit Bera do you have any idea how to do it – RickyN73 Jun 29 '15 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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