2014-04-11 217 views
0

我想覆蓋Magento中的核心控制器中的函數。有問題的控制器\magento\app\code\core\Mage\Customer\controllers\AccountController.php覆蓋Magento中的核心控制器

我創建下列文件:

  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)

任何人都可以請我在做什麼錯了,因爲沒有被覆蓋

+0

好吧,我會檢查 –

回答

0

你好嘗試進行代碼config.xml中的功能createPostAction建議是

<?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'); 
    } 
} 
?> 
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是罰款。不需要改變..乾杯..!

+0

我都試過,但仍然沒有關係沒有工作。任何其他建議? – user3305256