在 Magento 中覆盖/重载控制器类

发布于 2024-10-04 03:25:03 字数 4879 浏览 4 评论 0原文

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

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

<?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";
}
}

这是我第一次尝试重载控制器,所以我什至不确定我是否做得正确。我想做的最后一件事就是向核心添加代码......

提前致谢。

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

<?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

<?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...

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我恋#小黄人 2024-10-11 03:25:03

当我使控制器过载时,我不会直接使它们过载,这在过去对我来说一直是个问题。所以你的 config.xml 是正确的。

controller/Catalog/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

When I'm overloading controllers I don't overload them directly, that has been problematic for me in the past. So your config.xml is correct.

controller/Catalog/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

The original controller hasn't been replaced, just superseded. If an action other than view is required the router will not find it in your class, nor it's ancestors, and default to Mage_Catalog_CategoryController which is still accessible.

末が日狂欢 2024-10-11 03:25:03

我认为,您的问题是LHM_CategoryLanding_Catalog。它可能应该是 LHM_CategoryLanding ,您的控制器文件夹就在其中。

您试图告诉它使用哪个模块,因此该模块是 LHM_CategoryLanding 而不是 LHM_CategoryLanding_Catalog

I think, your problem is LHM_CategoryLanding_Catalog. It probably should be LHM_CategoryLanding where you have your controllers folder in there.

You are trying to tell it what module to use so the module is LHM_CategoryLanding not LHM_CategoryLanding_Catalog.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文