Magento - 如何扩展前端控制器

Magento - How to extend Front Controller

我需要更改 Magento 函数 _checkBaseUrl,发现于:

app/code/core/Mage/Core/Controller/Varien/Front.php

作为最佳实践,我尝试用我自己的模块扩展它,所以我没有编辑核心代码和文件,但它不起作用。如果我在核心文件中进行更改,我会得到所需的响应,但是在使用它时它不起作用。我的模块在 Configuration > Advanced > Advanced

中显示为已启用

文件如下:

etc/modules/Me_Coreextend.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Me_Coreextend>
            <active>true</active>
            <codePool>local</codePool>
        </Me_Coreextend>
    </modules>
</config>

app/code/local/Me/Coreextend/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Me_Coreextend>
            <version>1.0</version>
        </Me_Coreextend>
  </modules>
    <frontend>
        <routers>
            <core>
                <args>
                    <modules>
                         <Me_Coreextend before="Mage_Core">Me_Coreextend</Me_Coreextend>
                    </modules>
                </args>
            </core>
        </routers>
    </frontend>
</config>

app/code/local/Me/Coreextend/Controller/Varien/Front.php

我在此文件中仅包括 _checkBaseUrl 函数,而不是 app/code/core/Mage/Core/Controller/Varien/Front.php 的全部内容 - 这是否正确? (我也尝试将其全部包含在内,但仍然无效)

include_once('Mage/Core/Controller/Varien/Front.php');
class Me_Coreextend_Controller_Varien_Front extends Mage_Core_Controller_Varien_Front
{
    protected function _checkBaseUrl($request)
    {
        // custom changes...
    }
}

我做错了什么吗?

Magento 不提供覆盖文件夹 "Controller" 中的控制器文件的功能,您只能覆盖 "controllers" 文件夹中的控制器文件

只需复制文件即可加载

app/code/local/Mage/Core/Controller/Varien/Front.php

您不能扩展 Magento 的前端控制器,因为它不是 "standard" 服务控制器(注意它在 Controller 文件夹中而不是 controllers 文件夹中)。正如您在 Mage_Core_Model_App class 中看到的 run 方法调用的 _initFrontController 方法:

$this->_frontController = new Mage_Core_Controller_Varien_Front();

您只能复制项目本地文件夹中的文件:

app/code/local/Mage/Core/Controller/Varien/Front.php

并根据您的业务要求安全地编辑文件。