如何扩展Magento核心控制器

redmaomail 2024-07-24 18:13 阅读数 69 #Magento

红帽云邮外贸主机

今天我将演示如何在不弄乱核心文件本身的前提下编辑Magento核心模块。

我选择Magento Customer模块下的Account控制器作为本文的示例。你要先找出它在Magento核心文件夹里的路径(完整路径是:app/code/core/Mage/Customer/controllers/AccountController.php)。

首先创建有类似文件结构的文件:

app/code/local/Alwayly/Coreextended/controllers/Frontend/Customer/AccountController.php。(当然,你可以把Alwayly替换成你想要的命名空间,Coreextended换成其它模块名,但你需要对其它部分做相应的修改)。

接着创建我们模块的xml文件:app/code/local/Alwayly/Coreextended/etc/config.xml。将下面的代码写入对应的文件中:

1.AccountController.php:

< ?php
require_once Mage::getModuleDir('controllers', 'Mage_Customer').DS.'AccountController.php';
//we need to add this one since Magento wont recognize it automatically

class Alwayly_Coreextended_Frontend_Customer_AccountController extends Mage_Customer_AccountController
{//here, you extended the core controller with our public function indexAction()
{
parent::indexAction();
//you can always use default functionality
}

public function myactionAction()
{
//my code
//you can write your own methods / actions
}

public function mymethod()
{
//my code
//you can write your own methods
}

public function loginAction()
{
//finally you can write your code that will rewrite the whole core method
//and you can call for your own methods, as you have full control over core controller
}
}

2.config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <alwayly_coreextended>
            <version>0.2.0</version>
        </alwayly_coreextended>
    </modules>
    <frontend>
        <routers>
            <customer>
                <args>
                    <modules>
                        <alwayly_coreextended before="Mage_Customer_AccountController">
                               Alwayly_Coreextended_Frontend_Customer
                        </alwayly_coreextended>
                    </modules>
                </args>
            </customer>
        </routers>
    </frontend>
</config>

3.Alwayly_Coreextended.xml:

< ?xml version="1.0"?>
<!--we need to enable this module as any other if-->
<!--you wish to do it as standalone module extension-->
<config>
    <modules>
        <alwayly_coreextended>
            <active>true</active>
            <codepool>local</codepool>
        </alwayly_coreextended>
    </modules>
</config>

电商网站开发服务


红帽云邮外贸主机

分享到:
版权声明:本站内容源自互联网,如有内容侵犯了你的权益,请联系删除相关内容。
    红帽云邮外贸主机
热门
    红帽云邮外贸主机
    红帽云邮外贸主机