覆盖Magento中的核心控制器

时间:2014-04-11 14:03:58

标签: magento

我正在尝试覆盖Magento核心控制器中的一个函数。有问题的控制器是\magento\app\code\core\Mage\Customer\controllers\AccountController.php

我创建了以下文件:

  1. app/code/local/MyNameSpace/MyModule/etc/config.xml - http://codeshare.io/PJ0Cg

  2. app/code/local/MyNameSpace/MyModule/controllers/Customer/AccountController.php - http://codeshare.io/B4ciV

  3. app/etc/modules/MyNameSpace _MyModule.xml - http://codeshare.io/OUEdV(由于我的声誉低于10,我无法发布此链接)

  4. 任何人都可以告诉我我做错了什么,因为函数createPostAction没有被覆盖

3 个答案:

答案 0 :(得分:0)

你好试试代码config.xml是

<?xml version="1.0" encoding="utf-8"?>
<config>
        <modules>
        <Amit_Popuplogin>
            <version>1.0.0</version>
        </Amit_Popuplogin>
        </modules>
        <global>
        <rewrite>
                <popuplogin>
                    <from><![CDATA[#^/customer/account/#]]></from>
                    <to><![CDATA[/popuplogin/account/]]></to>
            </popuplogin>
            </rewrite>
        </global>
        <frontend>
            <routers>
                <popuplogin>
                    <use>standard</use>
                    <args>
                        <module>Amit_Popuplogin</module>
                        <frontName>popuplogin</frontName>
                    </args>
                </popuplogin>
            </routers>
        </frontend>
</config>

控制器路径和控制器代码是

需要添加predispath()alwasys .....

<?php
require_once Mage::getModuleDir('controllers', 'Mage_Customer').DS.'AccountController.php';
class Amit_Popuplogin_AccountController extends Mage_Customer_AccountController
{
    public function indexAction()
    {
    parent::indexAction();  
    }
        protected $_cookieCheckActions = array('loginPost', 'createpost','ajaxloginPost');

   protected $_validActions = array('create','login','logoutSuccess','forgotpassword','forgotpasswordpost','confirm','confirmation','resetpassword','resetpasswordpost');
      protected $_customActions = array('signupformpopup','ajaxloginPost','ajaxCreate','ajaxForgotPassword','logout');

     public function preDispatch()
     {
           $action = $this->getRequest()->getActionName();

           if (preg_match('/^('.$this->_getCustomActions().')/i', $action))
           {
            $this->getRequest()->setActionName($this->_validActions[1]);
           }

           parent::preDispatch();

           /**
            * Parent check is complete, reset request action name to origional value
            */
           if ($action != $this->getRequest()->getActionName())
           {
            $this->getRequest()->setActionName($action);
           }

           if (!$this->getRequest()->isDispatched()) {
            return;
           }

           if (!preg_match('/^('.$this->_getValidActions().')/i', $action)) {
            if (!$this->_getSession()->authenticate($this)) {
             $this->setFlag('', 'no-dispatch', true);
            }
           } else {
            $this->_getSession()->setNoReferer(true);
           }

     }
      protected function _getValidActions()
      {
      return implode("|", array_merge($this->_validActions, $this->_customActions));
      }

     /**
      * Gets custom action names and returns them as a pipe separated string
      *
      * @return string
      */
     protected function _getCustomActions()
     {
      return implode("|", $this->_customActions);
     }


 public function ajaxloginPostAction()
    {
        if ($this->_getSession()->isLoggedIn()) {

           $this->_redirect('*/*/');
            return;
        }
        $session = $this->_getSession();
        $result=array();

        if ($this->getRequest()->isPost()) {
            $login = $this->getRequest()->getPost('login');
            if (!empty($login['username']) && !empty($login['password'])) {
                try {
                    $session->login($login['username'], $login['password']);
                    if ($session->getCustomer()->getIsJustConfirmed()) {
                        $this->_welcomeCustomer($session->getCustomer(), true);
                    }
                    $result['success'] = true;
                    $result['redirecturl'] = Mage::getUrl('customer/account/edit');
                    $result['message'] = Mage::helper('customer')->__('You are successfully loged in.');
                } catch (Mage_Core_Exception $e) {
                    switch ($e->getCode()) {
                        case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED:
                            /*$message = Mage::helper('customer')->__('This account is not confirmed. <a href="%s">Click here</a> to resend confirmation email.', Mage::helper('customer')->getEmailConfirmationUrl($login['username']));*/
                            $result['success'] = false;
                            $result['message'] = Mage::helper('customer')->__('This account is not confirmed.');
                            break;
                        case Mage_Customer_Model_Customer::EXCEPTION_INVALID_EMAIL_OR_PASSWORD:
                            $message = $e->getMessage();
                            $result['success'] = false;
                            $result['message'] = Mage::helper('customer')->__($message);
                            break;
                        default:
                            $message = $e->getMessage();
                            $result['success'] = false;
                            $result['message'] = Mage::helper('customer')->__($message);
                    }
                    //$session->addError($message);
                    $session->setUsername($login['username']);
                } catch (Exception $e) {
                    // Mage::logException($e); // PA DSS violation: this exception log can disclose customer password
                }
            } else {
                //$session->addError($this->__('Login and password are required.'));
                $result['success'] = false;
                $result['message'] = Mage::helper('customer')->__('Login and password are required.');
            }
        }
        $this->getResponse()->setBody(Zend_Json::encode($result));
        //$this->_loginPostRedirect();
    }

 /**
     * Forgot customer password action
     */
    public function ajaxforgotPasswordPostAction()
    {


        $result= array();

        $email = (string) $this->getRequest()->getPost('email');
        if ($email) {
            if (!Zend_Validate::is($email, 'EmailAddress')) {
                $this->_getSession()->setForgottenEmail($email);
                 $result['success'] = false;
                 $result['message'] =  $this->_getSession()->addError($this->__('Invalid email address.'));
                  $this->getResponse()->setBody(Zend_Json::encode($result));
               //$this->_redirect('*/*/forgotpassword');
                return;
            }

            /** @var $customer Mage_Customer_Model_Customer */
            $customer = Mage::getModel('customer/customer')
                ->setWebsiteId(Mage::app()->getStore()->getWebsiteId())
                ->loadByEmail($email);

            if ($customer->getId()) {
                try {
                    $newResetPasswordLinkToken = Mage::helper('customer')->generateResetPasswordLinkToken();
                    $customer->changeResetPasswordLinkToken($newResetPasswordLinkToken);
                    $customer->sendPasswordResetConfirmationEmail();
                } catch (Exception $exception) {
                    $this->_getSession()->addError($exception->getMessage());
                    $this->_redirect('*/*/forgotpassword');
                    return;
                }
            }
            $this->_getSession()
                ->addSuccess(Mage::helper('customer')->__('If there is an account associated with '.$email.' you will receive an email with a link to reset your password.', Mage::helper('customer')->htmlEscape($email)));
                $result['success'] = true;
               $result['message'] =  Mage::helper('customer')->__('If there is an account associated with '.$email.' you will receive an email with a link to reset your password.');
                $this->getResponse()->setBody(Zend_Json::encode($result));
           // $this->_redirect('*/*/');
            return;
        } else {


            $result['success'] = false;
               $result['message'] =  $this->_getSession()->addError($this->__('Please enter your email.'));
                $this->getResponse()->setBody(Zend_Json::encode($result));
           // $this->_redirect('*/*/forgotpassword');
            return;
        }
    }
    /**
    * Customer logout action
    */
    public function logoutAction()
    {
      $this->_getSession()->logout()
            ->setBeforeAuthUrl(Mage::getUrl());
        $result= array();
        $result['success'] = true;
        $this->getResponse()->setBody(Zend_Json::encode($result));
       // $this->_redirect('*/*/logoutSuccess');
    }
}
?>

答案 1 :(得分:0)

我认为你在app/code/local/MyNameSpace/MyModule/etc/config.xml中错过了很多  试试这个代码..

<强> config.xml中:

<?xml version="1.0"?>
<config>
  <modules>
    <MyNameSpace_MyModule>
      <version>0.1.0</version>
    </MyNameSpace_MyModule>
  </modules>
  <frontend>
    <routers>
      <mymodule>
        <use>standard</use>
          <args>
            <module>MyNameSpace_MyModule</module>
            <frontName>mymodule</frontName>
          </args>
      </mymodule>
    </routers>
  </frontend>
  <global>
        <rewrite>        
            <mynamespace_mymodule_customer_accountcontroller>
                <from><![CDATA[#^/customer/account/#]]></from> <!-- Mage_Customer_AccountController  -->
                <to>/mymodule/customer_account/</to> <!-- MyNameSpace_MyModule_Customer_AccountController  -->
            </mynamespace_mymodule_customer_accountcontroller>
        </rewrite>
    <helpers>
      <mymodule>
        <class>MyNameSpace_MyModule_Helper</class>
      </mymodule>
    </helpers>
  </global>
  <admin>
    <routers>
      <mymodule>
        <use>admin</use>
        <args>
          <module>MyNameSpace_MyModule</module>
          <frontName>mymodule</frontName>
        </args>
      </mymodule>
    </routers>
  </admin>
</config> 

<强> AccountController.php:

<?php
require_once "Mage/Customer/controllers/AccountController.php";  
class MyNameSpace_MyModule_Customer_AccountController extends Mage_Customer_AccountController{

}

我认为你不需要助手。但是这个好的做法在创建模块时声明了帮助,

app/code/local/MyNameSpace/MyModule/Helper/

<强> Data.php:

<?php
class MyNameSpace_MyModule_Helper_Data extends Mage_Core_Helper_Abstract
{
}

MyNameSpace _MyModule.xml没问题。无需改变..干杯..!

答案 2 :(得分:0)

看看@

在你的config.xml中

.....
<modules>
    <inchoo_coreextended before="Mage_Customer_AccountController"> Inchoo_Coreextended_Frontend_Customer</inchoo_coreextended>
</modules>
.....