在Magento中覆盖/重载控制器类 [英] Overwriting / Overloading a controller class in Magento

查看:281
本文介绍了在Magento中覆盖/重载控制器类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图重载/覆盖Magento中的CategoryController类,但我每次遇到404错误。我已经遵循了许多我在网上找到的指南,但仍然似乎很短。

I'm trying to overload / overwrite the CategoryController class in Magento, but am coming up against a 404 error every time. I've followed many guidelines that I've found on the net but still seem to be coming up short.

etc / config.xml

etc/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>

controller / Catalog / CategoryController.php

controller/Catalog/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";
}
}

这是我第一次尝试重载一个控制器,所以我甚至不知道如果我做的正确。我想做的最后一件事是向核心代码添加代码...

This is the first time I've tried to overload a controller so I'm not even sure if I'm doing it right. The last thing I want to be doing is adding code to the core...

提前感谢。

推荐答案

尝试参考: http://prattski.com/2010/06/24/magento-overriding-core-files-blocks-models-resources-controllers/

这篇关于在Magento中覆盖/重载控制器类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆