如何重写控制器AccountController


12

我需要重写一个方法控制器

Core/Mage/Customer/controllers/AccountController.php 

并添加新方法。由于此控制器的编辑是错误的-应该覆盖它。

根据项目要求,控制器必须位于

local/New/Mage/Customer/controllers/AccountController.php 

要做到这一点,创建一个文件的配置,但地址customer/account/testcustomer/account /ajax没有回应,而customer/account/login这是不覆盖。请协助执行。

app / app / etc / modules / New_Mage_Customer.xml

<?xml version="1.0"?>
<config>
 <modules>
      <New_Mage_Customer>
           <active>true</active>
           <codePool>local</codePool>
      </New_Mage_Customer>
  </modules>
</config>

app / code / local / New / Mage / Customer / etc / config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <New_Mage_Customer>
            <version>0.0.1</version>
        </New_Mage_Customer>
    </modules>
    <frontend>
        <routers>
            <customer>
                <args>
                    <modules>
                        <new_customer before="Mage_Customer">New_Mage_Customer</new_customer>
                    </modules>
                </args>
            </customer>
        </routers>
    </frontend>
</config>

app / code / local / New / Mage / Customer / controllers / AccountController.php

<?php

/**
 * Customer account controller
 */
require_once 'Mage/Customer/controllers/AccountController.php';

class New_Mage_Customer_AccountController extends Mage_Customer_AccountController {

    public function ajaxAction() {
        echo 'ajax!!';
    }

    public function testAction() {
        echo 'test222';
    }

    public function loginAction() {
        echo 'index';
    }

}

谢谢!


Answers:


22

文件名将为app / etc / modules / New_Mage.xml

<?xml version="1.0"?>
<config>
 <modules>
      <New_Mage>
           <active>true</active>
           <codePool>local</codePool>
      </New_Mage>
  </modules>
</config>

app / code / local / New / Mage / etc / config.xml中

<?xml version="1.0"?>
<config>
    <modules>
        <New_Mage>
            <version>0.0.1</version>
        </New_Mage>
    </modules>
    <frontend>
        <routers>
            <customer>
                <args>
                    <modules>
                        <New_Mage before="Mage_Customer">New_Mage</New_Mage>
                    </modules>
                </args>
            </customer>
        </routers>
    </frontend>
</config>

控制器将 app / code / local / New / Mage / controllers / AccountController.php

/**
 * Customer account controller
 */
require_once Mage::getModuleDir('controllers', 'Mage_Customer') . DS . 'AccountController.php';

class New_Mage_AccountController extends Mage_Customer_AccountController {

    public function ajaxAction() {
        echo 'ajax!!';
    }

    public function testAction() {
        echo 'test222';
    }

    public function loginAction() {
        echo 'index';
    }

}

参考


这不会覆盖所有customer请求,包括example.com/customer/address/new/吗?如果此新模块中没有地址控制器,则所有/customer/address/*请求现在都将为404
Nick Rolando
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.