Friday 9 August 2013

Custom search engine friendly (SEF) URL In Magento.


Custom SEF helper method

class <Namespace>_<Module>_Helper_Data extends Mage_Core_Helper_Abstract
{
function custom_sef_url($id_path, $request_path, $target_path, $store_id ="0", $is_system=false){
$isrewrite = Mage::getModel('core/url_rewrite')->load($id_path,id_path);
    if($isrewrite->url_rewrite_id !=""){
      throw new Exception("Your Id path must be unique, given id path already in use !");
    }
      $rewrite = Mage::getModel('core/url_rewrite');
      $rewrite->setStoreId($store_id)
        ->setIdPath($id_path)
        ->setRequestPath($request_path)
        ->setTargetPath($target_path)
        ->setIsSystem($is_system)
        ->save();       
return;
}
}

Where should I call Custom SEF helper method?

Normally we need to create the custom URL once the new item is created, so we need to call this method in your “saveAction()” function after saving process. Let’s take FAQ items as example.

public function saveAction()
{

// .... faq saving process

try{
  $id_path = "faq/".$id;  // Id path must be unique
  $request_path = "fag/".$fag_title;
  $target_path  = "fag/index/index/id/".$id;

   Mage::helper('<module>')-&gt;custom_sef_url($id_path, $request_path, $target_path);
}
catch (Exception $e) {

 Mage::getSingleton('adminhtml/session')-&gt;addError($e-&gt;getMessage());

}

}

No comments:

Post a Comment