我正在尝试重载/覆盖Magento中的CategoryController类,但每次都遇到404错误。我遵循了许多我在网上发现的指导方针,但似乎仍然很短暂。
等/ config.xml中
<?xml version="1.0"?><config>
<modules>
<LHM_CategoryLanding>
<version>0.1.0</version>
</LHM_CategoryLanding>
</modules>
<!--<global>
<rewrite>
<lhm_categorylanding_catalog_category>
<from><![CDATA[#^/catalog/category/#]]></from>
<to>categorylanding/catalog_category/</to>
</lhm_categorylanding_catalog_category>
</rewrite>
</global>-->
<frontend>
<routers>
<catalog>
<args>
<modules>
<LHM_CategoryLanding before="Mage_Catalog">LHM_CategoryLanding_Catalog</LHM_CategoryLanding>
</modules>
</args>
</catalog>
<!--
<lhm_categorylanding>
<use>standard</use>
<args>
<module>LHM_CategoryLanding</module>
<frontName>categorylanding</frontName>
</args>
</lhm_categorylanding>
-->
</routers>
</frontend>
</config>
控制器/目录/ CategoryController.php
<?php
// This is needed since Varien used a layout that is not easily auto-loadable
require_once("Mage/Catalog/controllers/CategoryController.php");
class LHM_CategoryLanding_Catalog_CategoryController extends Mage_Catalog_CategoryController
{
/**
* Initialize requested category object
*
* @return Mage_Catalog_Model_Category
*/
protected function _initCatagory()
{
Mage::dispatchEvent('catalog_controller_category_init_before', array('controller_action'=>$this));
$categoryId = (int) $this->getRequest()->getParam('id', false);
if (!$categoryId) {
return false;
}
$category = Mage::getModel('catalog/category')
->setStoreId(Mage::app()->getStore()->getId())
->load($categoryId);
if (!Mage::helper('catalog/category')->canShow($category)) {
return false;
}
Mage::getSingleton('catalog/session')->setLastVisitedCategoryId($category->getId());
Mage::register('current_category', $category);
try {
Mage::dispatchEvent('catalog_controller_category_init_after', array('category'=>$category, 'controller_action'=>$this));
} catch (Mage_Core_Exception $e) {
Mage::logException($e);
return false;
}
return $category;
}
/**
* Category view action
*/
public function viewAction()
{
if ($category = $this->_initCatagory()) {
Mage::getModel('catalog/design')->applyDesign($category, Mage_Catalog_Model_Design::APPLY_FOR_CATEGORY);
Mage::getSingleton('catalog/session')->setLastViewedCategoryId($category->getId());
$update = $this->getLayout()->getUpdate();
$update->addHandle('default');
if (!$category->hasChildren()) {
$update->addHandle('catalog_category_layered_nochildren');
}
$this->addActionLayoutHandles();
$update->addHandle($category->getLayoutUpdateHandle());
$update->addHandle('CATEGORY_'.$category->getId());
if ($category->getPageLayout()) {
$this->getLayout()->helper('page/layout')
->applyHandle($category->getPageLayout());
}
$this->loadLayoutUpdates();
$update->addUpdate($category->getCustomLayoutUpdate());
$this->generateLayoutXml()->generateLayoutBlocks();
if ($category->getPageLayout()) {
$this->getLayout()->helper('page/layout')
->applyTemplate($category->getPageLayout());
}
if ($root = $this->getLayout()->getBlock('root')) {
$root->addBodyClass('categorypath-'.$category->getUrlPath())
->addBodyClass('category-'.$category->getUrlKey());
}
$this->_initLayoutMessages('catalog/session');
$this->_initLayoutMessages('checkout/session');
/* START ===== Pete T additional code. Need to put this in override!! */
if($category->getLevel()==2){
$category->setDisplayMode('PAGE');
}
/* END ======= */
$this->renderLayout();
}
elseif (!$this->getResponse()->isRedirect()) {
$this->_forward('noRoute');
}
}
protected function _getRealModuleName()
{
return "LHM_CategoryLanding";
}
}
这是我第一次尝试重载控制器,所以我甚至不确定我是否正确操作。我想要做的最后一件事是向核心添加代码......
提前致谢。
答案 0 :(得分:1)
尝试参考:http://prattski.com/2010/06/24/magento-overriding-core-files-blocks-models-resources-controllers/
答案 1 :(得分:0)
我认为,您的问题是LHM_CategoryLanding_Catalog
。可能应该是LHM_CategoryLanding
,其中有控制器文件夹。
您试图告诉它使用哪个模块,因此模块为LHM_CategoryLanding
而不是LHM_CategoryLanding_Catalog
。
答案 2 :(得分:0)
当我超载控制器时,我没有直接超载它们,这对我来说过去一直存在问题。所以你的config.xml
是正确的。
控制器/目录/ CategoryController.php
<?php
// No need for an include/require - it will only break when Compilation is enabled.
class LHM_CategoryLanding_Catalog_CategoryController extends Mage_Core_Controller_Front_Action
{
// Continue as normal
原始控制器尚未更换,只是被取代。如果需要view
以外的操作,路由器将不会在您的班级中找到它,也不会是它的祖先,并且默认为Mage_Catalog_CategoryController
仍可访问。