2016-02-16 78 views
0

我的意图很简单,我只想让我的网页在不同的格式事件调度

www.my-site.com/product-name.html - 将加载页面的HTML

,但我希望有一个JSON对象,当我键入

www.my-site.com/product-name.json

如果他们的任何事件呈现404页之前磁调度,这是非常有益的

或我不得不重写应用程序/代码/核心/法师/ CMS /控制器/ indexcontroller.php 或多或少

public function defaultNoRouteAction() 
{ 
    $this->getResponse()->setHeader('HTTP/1.1','404 Not Found'); 
    $this->getResponse()->setHeader('Status','404 File not found'); 

    $this->loadLayout(); 
    $this->renderLayout(); 
} 

但我不知道如何。

回答

0

我做过类似的东西,调用一个产品网址.XML,而不是html的导出时的产品数据。

我已经添加了一个观察者监听产品保存的事件,这增加了产品的XML网址为core_url_rewrite。 在这种情况下,必须与.XML

例如取代的.htm(L)

产品name.htm - >目录/产品/视图/ ID/4 - >负载产物与ID 4和渲染产品视图模板,magento的核心

产品name.xml的 - > mycustommodule /产品/查看/ ID/4 - >装载的产品ID为4和渲染XML,自定义模块

所以,你必须添加一个名为“mycustommodule”另一个模块来处理XML输出。

干杯!

0

我的解决方案,我发现是下面,它完美地工作,但不知道可以肯定的是它的磁WAY

我重写Mage_Cms_IndexController noRoute方法,

public function noRouteAction($coreRoute = null) { 

    // getting current url 
     $url = Mage::helper('core/url')->getCurrentUrl(); 



      if (strstr($url,".jsonp") || strstr($url,".json") || strstr($url,".xml")) { 

      // geting file-type like json/xml 
      $format = substr(strrchr($url, "."),1); 
      // product-handle 
       $elem = substr(strrchr($url, "/"),1, strpos(strrchr($url, "/") , ".")-1); 

       switch ($format) { 
        case 'json': case 'JSON': case 'jsonp': case 'JSONP': 
         $this->getResponse()->setHeader('Content-type', 'application/json'); break; 

        case 'xml': case 'XML': 
         $this->getResponse()->setHeader('Content-type', 'application/xml'); break; 

       } 

       // loding product byproduct-handle 
       $rewrite = Mage::getModel('core/url_rewrite')->setStoreId(Mage::app()->getStore()->getId())->loadByRequestPath($elem . ".html"); 
       $pid = $rewrite->getProductId(); 
       $cat = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->loadByAttribute('url_key', $elem ); 

       if (strpos($url,"hs_review")) { echo Mage::getModel("Hs_Json/review")->wrapper($pid,$format);return;} 
       if ($pid) { echo Mage::getModel("Hs_Json/product")->wrapper($pid,$format);return; } 
       if ($cat) { echo Mage::getModel("Hs_Json/category")->wrapper($cat->getId(),$format);return;} 


      } else { 

      $this->getResponse()->setHeader('HTTP/1.1','404 Not Found'); 
      $this->getResponse()->setHeader('Status','404 File not found'); 

      $pageId = Mage::getStoreConfig(Mage_Cms_Helper_Page::XML_PATH_NO_ROUTE_PAGE); 
      if (!Mage::helper('cms/page')->renderPage($this, $pageId)) { 
       $this->_forward('defaultNoRoute'); 
      } 

      } 

    } 

}